﻿Imports System.Reflection

''' <summary>The data binding class will handle the data integration to the FrameworkObjects class</summary>
Public Class Databinding

#Region "**Enums and Constants**"

    ''' <summary>Enum for database connection type to be used in data binding to the object class. </summary>
    Public Enum ConnectionTypes
        ''' <summary>ADO.NET SQL Connection Type</summary>
        SQLDatabase = 1
        ''' <summary>ADO.NET OLE Connection Type</summary>
        OLEDatabase = 2
    End Enum

    ''' <summary>Enum used for determining which data binding methods to call. </summary>
    Public Enum DataBindingMethods
        ''' <summary> This method reads the data as the objects use it in a very linear fashion. Can be slow.</summary>
        Standard = 1
        ''' <summary>This method allows the developer to code very specific and structured Select/Update/Insert/Delete commands.
        ''' There are only shell methods for these as the code will be specific to the developer.
        ''' </summary>
        Customized = 2
        ''' <summary>This method loads all the schedule data into memory then uses LINQ to parse the data. This is a very fast method. 
        ''' You can modify the existing select statements to apply specific logic.
        ''' </summary>
        InMemory = 3
    End Enum
#End Region

#Region "**Properties and Local Variables**"

#Region "--Local Variables--"

    'Global variables used to enable/disable auto-updating which occurs
    'on the dbiWSF.FrameWork objects set methods. We don't want to update a data cell when
    'we are loading and setting the mapped property.
    ''' <summary>Local variable used to indicate the state of data mapping to prevent circular recursive calls</summary>
    Dim IsMappingData As Boolean
    ''' <summary>Global variable used to indicate the state of the data binding class</summary>
    Public IsLoadingData As Boolean

    'A global reference to the objWSFObjects which handles
    'the synchronization to the FrameworkObjects
    Dim l_WSFObject As dbiWSF.FrameworkObjects.objWSFObjects

    'Local data adapters (declared as objects so we can use either
    'OLE or SQL data adapters).
    Dim l_daWarehouses As Object
    Dim l_daLoadingDocks As Object
    Dim l_daShipments As Object
    Dim l_daOrderHeaders As Object
    Dim l_daOrderDetails As Object
    Dim l_daLoadingDockExceptions As Object
    Dim l_daExceptionDockKeys As Object
    Dim l_daRecurrences As Object
    Dim l_daRecurrenceObjectKeys As Object
    Dim l_daCargoTypes As Object
    Dim l_daLoadingDockCargoKeys As Object

    'Local data tables (we surface these through public properties so developers
    'can have access to the underlying tables in case they want more control over
    'the data binding than that provided)
    Dim l_dtWarehouses As New DataTable("Warehouses")
    Dim l_dtLoadingDocks As New DataTable("LoadingDocks")
    Dim l_dtShipments As New DataTable("Shipments")
    Dim l_dtOrderHeaders As New DataTable("OrderHeaders")
    Dim l_dtOrderDetails As New DataTable("OrderDetails")
    Dim l_dtLoadingDockExceptions As New DataTable("LoadingDockExceptions")
    Dim l_dtExceptionDockKeys As New DataTable("ExceptionDockKeys")
    Dim l_dtRecurrences As New DataTable("Recurrence")
    Dim l_dtRecurrenceObjectKeys As New DataTable("RecurrenceObjectKeys")
    Dim l_dtCargoTypes As New DataTable("CargoTypes")
    Dim l_dtLoadingDockCargoKeys As New DataTable("LoadingDockCargoKeys")

    'Local ColumnMapping object. The ColumnMappings allow developers to synchronize
    'column names to property names. By default if the same name is used in the 
    'database and the property then no mappings are needed.
    Dim l_cmapWarehouses As New ColumnMapping
    Dim l_cmapLoadingDocks As New ColumnMapping
    Dim l_cmapShipments As New ColumnMapping
    Dim l_cmapOrderHeaders As New ColumnMapping
    Dim l_cmapOrderDetails As New ColumnMapping
    Dim l_cmapLoadingDockExceptions As New ColumnMapping
    Dim l_cmapRecurrences As New ColumnMapping
    Dim l_cmapCargoTypes As New ColumnMapping

    'Local variables for the database connections.
    Dim l_ConnectionObject As Object
    Dim l_ConnectionString As String
    Dim l_ConnectionType As ConnectionTypes
    Dim l_TableMap As New Collections.Generic.Dictionary(Of String, String)
    Dim l_AutoUpdateData As Boolean
    Dim l_DataBindingMethod As DataBindingMethods
#End Region

#Region "--Properties--"

    ''' <summary>Used for database update state information. Do not remove.</summary>
    Public Property WSFObjects() As dbiWSF.FrameworkObjects.objWSFObjects
        Get
            Return l_WSFObject
        End Get
        Set(ByVal value As dbiWSF.FrameworkObjects.objWSFObjects)
            l_WSFObject = value
        End Set
    End Property

    ''' <summary>Gets or sets a connection object used in the data binding class. It can be either an ADO.NET SQL Client connection or OLEDB Connection</summary>
    Public Property ConnectionObject() As Object
        Get
            Return l_ConnectionObject
        End Get
        Set(ByVal value As Object)
            'If the ConnectionObject is set via a connection object, then
            'set the local variables accordingly.
            If value.GetType Is GetType(SqlClient.SqlConnection) Or value.GetType Is GetType(OleDb.OleDbConnection) Then
                l_ConnectionObject = value
                If value.GetType Is GetType(SqlClient.SqlConnection) Then
                    l_ConnectionType = ConnectionTypes.SQLDatabase
                    Dim p_SQLConn As SqlClient.SqlConnection = value
                    l_ConnectionString = p_SQLConn.ConnectionString
                Else
                    l_ConnectionType = ConnectionTypes.OLEDatabase
                    Dim p_OLEConn As OleDb.OleDbConnection = value
                    l_ConnectionString = p_OLEConn.ConnectionString
                End If
            End If
        End Set
    End Property

    ''' <summary>Determines if the data binding class should update the databases
    '''  when a property in the FrameworkObject class is updated.</summary>
    ''' <remarks>If this property is set to false, then a manual data update
    ''' be invoked to update the databases.</remarks>
    ''' <seealso cref="dbiWSF.Databinding.ManualUpdateDatabases"></seealso>
    Public Property AutoUpdateData() As Boolean
        Get
            Return l_AutoUpdateData
        End Get
        Set(ByVal value As Boolean)
            l_AutoUpdateData = value
        End Set
    End Property

    ''' <summary>Gets or sets the default connection string used in the data binding class.</summary>
    Public Property ConnectionString() As String
        Get
            Return l_ConnectionString
        End Get
        Set(ByVal value As String)
            l_ConnectionString = value
        End Set
    End Property

    ''' <summary>Gets or sets the connection type used in the data binding class.</summary>
    Public Property ConnectionType() As ConnectionTypes
        Get
            Return l_ConnectionType
        End Get
        Set(ByVal value As ConnectionTypes)
            l_ConnectionType = value
        End Set
    End Property

    ''' <summary>A collection that maps data tables to the various sub classes in the FrameworkObjects class.</summary>
    ''' <remarks>The collection accepts two strings, the key is the gettype(frameworkobject.class).ToString, 
    ''' the second string is the actual database table name.
    ''' </remarks>
    ''' <example>
    ''' Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objWarehouse).ToString, "Warehouses")<br/>
    ''' Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objLoadingDock).ToString, "LoadingDocks")
    ''' </example>
    Public Property TableMapping() As Collections.Generic.Dictionary(Of String, String)
        Get
            Return l_TableMap
        End Get
        Set(ByVal value As Collections.Generic.Dictionary(Of String, String))
            l_TableMap = value
        End Set
    End Property

    ''' <summary>Determines the <see cref="dbiWSF.Databinding.DataBindingMethods">data binding method</see> to use</summary>
    Public Property DataBindingMethod() As DataBindingMethods
        Get
            Return l_DataBindingMethod
        End Get
        Set(ByVal value As DataBindingMethods)
            l_DataBindingMethod = value
        End Set
    End Property

#Region "--DataAdapters--"

    ''' <summary>The data adapter for the warehouses</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterWarehouses() As Object
        Get
            Return l_daWarehouses
        End Get
        Set(ByVal value As Object)
            l_daWarehouses = value
        End Set
    End Property

    ''' <summary>The data adapter for the loading docks</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterLoadingDocks() As Object
        Get
            Return l_daLoadingDocks
        End Get
        Set(ByVal value As Object)
            l_daLoadingDocks = value
        End Set
    End Property

    ''' <summary>The data adapter for the shipments</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterShipments() As Object
        Get
            Return l_daShipments
        End Get
        Set(ByVal value As Object)
            l_daShipments = value
        End Set
    End Property

    ''' <summary>The data adapter for the order headers</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterOrderHeaders()
        Get
            Return l_daOrderHeaders
        End Get
        Set(ByVal value As Object)
            l_daOrderHeaders = value
        End Set
    End Property

    ''' <summary>The data adapter for the order details</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterOrderDetails()
        Get
            Return l_daOrderDetails
        End Get
        Set(ByVal value As Object)
            l_daOrderDetails = value
        End Set
    End Property

    ''' <summary>The data adapter for the loading dock exceptions</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterLoadingDockExceptions()
        Get
            Return l_daLoadingDockExceptions
        End Get
        Set(ByVal value As Object)
            l_daLoadingDockExceptions = value
        End Set
    End Property

    ''' <summary>The data adapter for the loading dock exception keys</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterExceptionDockKeys()
        Get
            Return l_daExceptionDockKeys
        End Get
        Set(ByVal value As Object)
            l_daExceptionDockKeys = value
        End Set
    End Property

    ''' <summary>The data adapter for the recurrences</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterRecurrences()
        Get
            Return l_darecurrences
        End Get
        Set(ByVal value As Object)
            l_darecurrences = value
        End Set
    End Property

    ''' <summary>The data adapter for the recurrence keys</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterRecurrenceObjectKeys()
        Get
            Return l_darecurrenceObjectKeys
        End Get
        Set(ByVal value As Object)
            l_darecurrenceObjectKeys = value
        End Set
    End Property

    ''' <summary>The data adapter for the cargo types</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterCargoTypes() As Object
        Get
            Return l_daCargoTypes
        End Get
        Set(ByVal value As Object)
            l_daCargoTypes = value
        End Set
    End Property

    ''' <summary>The data adapter for the loading dock cargo keys</summary>
    ''' <remarks>These are declared as generic objects as they can represent either 
    ''' ADO.SQL or ADO.OLE data adapters. To enable intellisense, you can cast these out to the appropriate
    ''' database type or cast them out in your code.
    ''' </remarks>
    Public Property dataadapterLoadingDockCargoKeys()
        Get
            Return l_daLoadingDockCargoKeys
        End Get
        Set(ByVal value)
            l_daLoadingDockCargoKeys = value
        End Set
    End Property
#End Region

#Region "--Column Mappings--"

    ''' <summary>Column mapping properties</summary>
    Public Property cmapWarehouses() As ColumnMapping
        Get
            Return l_cmapWarehouses
        End Get
        Set(ByVal value As ColumnMapping)
            l_cmapWarehouses = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapLoadingDocks() As ColumnMapping
        Get
            Return l_cmapLoadingDocks
        End Get
        Set(ByVal value As ColumnMapping)
            l_cmapLoadingDocks = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapShipments() As ColumnMapping
        Get
            Return l_cmapShipments
        End Get
        Set(ByVal value As ColumnMapping)
            l_cmapShipments = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapOrderHeaders() As ColumnMapping
        Get
            Return l_cmapOrderHeaders
        End Get
        Set(ByVal value As ColumnMapping)
            l_cmapOrderHeaders = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapOrderDetails() As ColumnMapping
        Get
            Return l_cmapOrderDetails
        End Get
        Set(ByVal value As ColumnMapping)
            l_cmapOrderDetails = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapLoadingDockExceptions() As ColumnMapping
        Get
            Return l_cmapLoadingDockExceptions
        End Get
        Set(ByVal value As ColumnMapping)
            l_cmapLoadingDockExceptions = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapRecurrences() As dbiWSF.Databinding.ColumnMapping
        Get
            Return l_cmapRecurrences
        End Get
        Set(ByVal value As dbiWSF.Databinding.ColumnMapping)
            l_cmapRecurrences = value
        End Set
    End Property

    ''' <summary>Column mapping properties</summary>
    Public Property cmapCargoTypes() As dbiWSF.Databinding.ColumnMapping
        Get
            Return l_cmapCargoTypes
        End Get
        Set(ByVal value As dbiWSF.Databinding.ColumnMapping)
            l_cmapCargoTypes = value
        End Set
    End Property

#End Region

#Region "--DataTables--"

    ''' <summary>The data table for the warehouses</summary>
    Public Property datatableWarehouses() As DataTable
        Get
            Return l_dtWarehouses
        End Get
        Set(ByVal value As DataTable)
            l_dtWarehouses = value
        End Set
    End Property

    ''' <summary>The data table for the loading docks</summary>
    Public Property datatableLoadingDocks() As DataTable
        Get
            Return l_dtLoadingDocks
        End Get
        Set(ByVal value As DataTable)
            l_dtLoadingDocks = value
        End Set
    End Property

    ''' <summary>The data table for the shipments</summary>
    Public Property datatableShipments() As DataTable
        Get
            Return l_dtShipments
        End Get
        Set(ByVal value As DataTable)
            l_dtShipments = value
        End Set
    End Property

    ''' <summary>The data table for the order headers</summary>
    Public Property datatableOrderHeaders() As DataTable
        Get
            Return l_dtOrderHeaders
        End Get
        Set(ByVal value As DataTable)
            l_dtOrderHeaders = value
        End Set
    End Property

    ''' <summary>The data table for the order details</summary>
    Public Property datatableOrderDetails() As DataTable
        Get
            Return l_dtOrderDetails
        End Get
        Set(ByVal value As DataTable)
            l_dtOrderDetails = value
        End Set
    End Property

    ''' <summary>The data table for the loading dock exceptions</summary>
    Public Property datatableLoadingDockExceptions() As DataTable
        Get
            Return l_dtLoadingDockExceptions
        End Get
        Set(ByVal value As DataTable)
            l_dtLoadingDockExceptions = value
        End Set
    End Property

    ''' <summary>The data table for the loading dock exception keys</summary>
    Public Property datatableExceptionDockKeys() As DataTable
        Get
            Return l_dtExceptionDockKeys
        End Get
        Set(ByVal value As DataTable)
            l_dtExceptionDockKeys = value
        End Set
    End Property

    ''' <summary>The data table for the recurrences</summary>
    Public Property datatableRecurrences() As DataTable
        Get
            Return l_dtrecurrences
        End Get
        Set(ByVal value As DataTable)
            l_dtrecurrences = value
        End Set
    End Property

    ''' <summary>The data table for the recurrence keys</summary>
    Public Property datatableRecurrenceObjectKeys() As DataTable
        Get
            Return l_dtrecurrenceObjectKeys
        End Get
        Set(ByVal value As DataTable)
            l_dtrecurrenceObjectKeys = value
        End Set
    End Property

    ''' <summary>The data table for the cargo types</summary>
    Public Property datatableCargoTypes() As DataTable
        Get
            Return l_dtCargoTypes
        End Get
        Set(ByVal value As DataTable)
            l_dtCargoTypes = value
        End Set
    End Property

    ''' <summary>The data table for the loading dock cargo keys</summary>
    Public Property datatableLoadingDockCargoKeys() As DataTable
        Get
            Return l_dtLoadingDockCargoKeys
        End Get
        Set(ByVal value As DataTable)
            l_dtLoadingDockCargoKeys = value
        End Set
    End Property

#End Region


#End Region

#End Region

#Region "**Public Methods and Functions**"

    ''' <summary>
    ''' Instantiates a new data binding object. We call the base new from the overloads as it houses the mapping objects.
    ''' </summary>
    ''' <remarks>This method creates the table bindings and sets default properties on the data binding object.</remarks>
    Public Sub New()
        'Table mappings - <Modify> table names if different from default
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objWarehouse).ToString, "Warehouses")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objLoadingDock).ToString, "LoadingDocks")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objShipment).ToString, "Shipments")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objOrderHeader).ToString, "OrderHeaders")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objOrderDetail).ToString, "OrderDetails")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objCargoType).ToString, "CargoTypes")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objLoadingDockException).ToString, "LoadingDockExceptions")
        Me.TableMapping.Add(GetType(dbiWSF.FrameworkObjects.objRecurrence).ToString, "Recurrence")


        Me.AutoUpdateData = True

        Me.DataBindingMethod = DataBindingMethods.InMemory
        Me.ConnectionType = ConnectionTypes.SQLDatabase


        'Each table can have a cmap entry that will map a column
        'name to a property name. By default the dbiWSF ships with tables
        'where the property names match the column names. For integration and database
        'admin purposes this may not always be the case.


        'Note: ColumnName, PropertyName for mapping
        'Me.cmapTableName.Mapping.Add(ColumnName as string, PropertyName as string)

        'Example:
        'Me.cmapLoadingDocks.Mapping.Add("LocalAddress", "Address")



    End Sub

    ''' <summary>
    ''' Overloaded, instantiates a new data binding class passing in a default parent objWSFObjects, connection string and connection type
    ''' </summary>
    ''' <param name="objWSFObjects">A global objWSFObjects class used to sync the data binding to the framework objects</param>
    ''' <param name="ConnString">The connection string used to connect to a database</param>
    ''' <param name="ConnType">The type of connection to establish</param>
    ''' <remarks></remarks>
    Public Sub New(ByRef objWSFObjects As dbiWSF.FrameworkObjects.objWSFObjects, ByVal ConnString As String, ByVal ConnType As ConnectionTypes)
        Me.New()
        ConnectionString = ConnString
        ConnectionType = ConnType
    End Sub

    ''' <summary>
    ''' Loads the data for the attached FrameworkObjects.
    ''' </summary>
    ''' <param name="StartDateTime">Optional: The start date/time of the shipments to load</param>
    ''' <param name="EndDateTime">Optional: The end date/time of the shipments to load</param>
    ''' <returns>A collection of Warehouse objects with attached child objects</returns>
    ''' <remarks></remarks>
    Public Function LoadAllData(Optional ByVal StartDateTime As Date = Nothing, Optional ByVal EndDateTime As Date = Nothing) As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        IsLoadingData = True

        'Optional parameters start & end dates for select statements
        If StartDateTime = Nothing Then StartDateTime = SqlTypes.SqlDateTime.MinValue
        If EndDateTime = Nothing Then EndDateTime = SqlTypes.SqlDateTime.MaxValue


        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)


        Me.datatableWarehouses.Clear()
        Me.datatableLoadingDocks.Clear()
        Me.datatableShipments.Clear()

        Select Case Me.ConnectionType
            Case ConnectionTypes.SQLDatabase
                Select Case Me.DataBindingMethod
                    Case DataBindingMethods.Standard
                        retWarehouses = LoadStandardSQL()
                    Case DataBindingMethods.Customized
                        retWarehouses = LoadCustomizedSQL()
                    Case DataBindingMethods.InMemory
                        retWarehouses = LoadInMemorySQL(StartDateTime, EndDateTime)

                    Case Else
                        retWarehouses = LoadInMemorySQL(StartDateTime, EndDateTime)
                End Select
            Case ConnectionTypes.OLEDatabase
                Select Case Me.DataBindingMethod
                    Case DataBindingMethods.Standard
                        retWarehouses = LoadStandardOLE()
                    Case DataBindingMethods.Customized
                        retWarehouses = LoadCustomizedOLE()
                    Case DataBindingMethods.InMemory
                        retWarehouses = LoadInMemoryOLE(StartDateTime, EndDateTime)
                    Case Else
                        retWarehouses = LoadInMemoryOLE(StartDateTime, EndDateTime)
                End Select
            Case Else
                Select Case Me.DataBindingMethod
                    Case DataBindingMethods.Standard
                        retWarehouses = LoadStandardSQL()
                    Case DataBindingMethods.Customized
                        retWarehouses = LoadCustomizedSQL()
                    Case DataBindingMethods.InMemory
                        retWarehouses = LoadInMemorySQL(StartDateTime, EndDateTime)
                    Case Else
                        retWarehouses = LoadInMemorySQL(StartDateTime, EndDateTime)
                End Select

        End Select

        IsLoadingData = False
        Return retWarehouses
    End Function

    ''' <summary>Gets a list of shipments that haven't been assigned to a loading dock</summary>
    ''' <remarks>The <see cref="dbiWSF.FrameworkObjects.objShipment.LoadingDockID">shipment loading dock ID</see> is an empty GUID</remarks>
    ''' <returns>A collection of shipment objects</returns>
    Public Function GetUnassignedShipments() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objShipment)
        Dim collUnassignedShipments As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objShipment)

        Dim colShipments = From selectItems As DataRow In datatableShipments.Rows _
              Where selectItems.Item(GetColumnMap(cmapShipments, "LoadingDockID")) = Guid.Empty _
              Select selectItems

        For Each curRow As DataRow In colShipments
            Dim newShipment As New dbiWSF.FrameworkObjects.objShipment(Me.WSFObjects)
            newShipment.AttachedDataRow = curRow
            MapData(newShipment, curRow, datatableShipments.Columns, cmapShipments)
            collUnassignedShipments.Add(newShipment)
        Next

        Return collUnassignedShipments
    End Function

    ''' <summary>Gets a list of Order Headers that haven't been assigned to a shipment</summary>
    ''' <remarks>The Shipment ID is an empty GUID</remarks>
    ''' <returns>A collection of OrderHeader objects</returns>
    Public Function GetUnassignedOrderHeaders() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objOrderHeader)

        Select Case Me.ConnectionType
            Case ConnectionTypes.OLEDatabase
                LoadUnassignedOrderHeadersOLE()
            Case ConnectionTypes.SQLDatabase
                LoadUnassignedOrderHeadersSQL()
        End Select


        Dim collUnassignedOrderHeaders As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objOrderHeader)

        Dim colOrderHeaders = From selectItems As DataRow In datatableOrderHeaders.Rows _
                              Where selectItems.Item(GetColumnMap(cmapOrderHeaders, "ShipmentID")) = Guid.Empty _
                              Select selectItems

        For Each curRow As DataRow In colOrderHeaders
            Dim newOrderHeader As New dbiWSF.FrameworkObjects.objOrderHeader(Me.WSFObjects)
            newOrderHeader.AttachedDataRow = curRow
            MapData(newOrderHeader, curRow, datatableOrderHeaders.Columns, cmapOrderHeaders)
            collUnassignedOrderHeaders.Add(newOrderHeader)

            Dim curOrderDetails() As DataRow = datatableOrderDetails.Select(Me.GetColumnMap(cmapOrderDetails, "OrderID") + " = '" + newOrderHeader.OrderID.ToString + "'")

            For Each curDetail As DataRow In curOrderDetails
                Dim newDetail As New dbiWSF.FrameworkObjects.objOrderDetail(Me.WSFObjects)
                newDetail.AttachedDataRow = curDetail
                MapData(newDetail, curDetail, datatableOrderDetails.Columns, cmapOrderDetails)
                newOrderHeader.OrderDetails.Add(newDetail)
            Next

        Next
        Return collUnassignedOrderHeaders
    End Function

    ''' <summary>Get the status of the data tables when the AutoUpdates are false</summary>
    ''' <remarks>This function will indicate if any table changes were made to the underlying data tables
    ''' requiring a manual update <see cref="dbiWSF.Databinding.ManualUpdateDatabases"></see></remarks>
    ''' <returns>Boolean: Indicating if the data tables have changes</returns>
    Public Function DataHasChanges() As Boolean
        Dim booChanges As Boolean = False
        If GetTableChanges(datatableWarehouses) = True Then booChanges = True
        If GetTableChanges(datatableLoadingDocks) = True Then booChanges = True
        If GetTableChanges(datatableLoadingDockExceptions) = True Then booChanges = True
        If GetTableChanges(datatableShipments) = True Then booChanges = True
        If GetTableChanges(datatableOrderHeaders) = True Then booChanges = True
        If GetTableChanges(datatableOrderDetails) = True Then booChanges = True
        If GetTableChanges(datatableRecurrences) = True Then booChanges = True
        If GetTableChanges(datatableLoadingDockCargoKeys) = True Then booChanges = True
        If GetTableChanges(datatableRecurrenceObjectKeys) = True Then booChanges = True
        If GetTableChanges(datatableCargoTypes) = True Then booChanges = True
        If GetTableChanges(datatableExceptionDockKeys) = True Then booChanges = True
        Return booChanges
    End Function

#End Region

#Region "**Supporting Functions and Methods**"

    ''' <summary>
    ''' Gets the table name associated with a dbiWSFObject
    ''' </summary>
    ''' <param name="ObjectToLoad">An instance of a dbiWSFObject</param>
    ''' <returns>String: Name of table set in the table mapping of the base sub New</returns>
    ''' <remarks></remarks>
    Public Function GetTableName(ByVal ObjectToLoad As Object) As String
        Dim retVal As String = ""
        Try
            retVal = Me.TableMapping.Item(ObjectToLoad.ToString)
        Catch ex As Exception
            Throw New Exception("Object not attached to table", ex)
        End Try

        Return retVal
    End Function

    ''' <summary>
    ''' Gets the table column name used to map the data to dbiWSFObject.Properties
    ''' </summary>
    ''' <param name="ColumnMap">The column map to use</param>
    ''' <param name="PropertyName">The property name to retrieve</param>
    ''' <returns>String: the column name that was mapped to that property</returns>
    ''' <remarks></remarks>
    Public Function GetColumnMap(ByVal ColumnMap As ColumnMapping, ByVal PropertyName As String) As String
        Dim retString As String = PropertyName
        Try
            If ColumnMap.Mapping.ContainsKey(PropertyName) Then
                retString = ColumnMap.Mapping.Item(PropertyName)
            End If
        Catch ex As Exception
            Throw ex '<GenericError> Modify as needed.
        End Try

        Return retString
    End Function

    ''' <summary>Using reflection we can take a data row and map it's data to the properties on the dbiWSFObject</summary>
    ''' <remarks>By default if you use the same table column names as the properties in the dbiWSFObjects or you use
    ''' the ColumnMapping option, then this method will map them directly to their counterparts.
    ''' If you are using third party tables or custom tables already created then you can recode this as needed</remarks>
    ''' <param name="ObjectToMapDataTo">The dbiWSF Object where the properties will be set</param>
    ''' <param name="DataRowToMapDataFrom">The data row that has the data to map</param>
    ''' <param name="DataColumnsForNames">A collection of data columns (usually datatable.columns) used in the
    ''' reflection to get the property names</param>
    ''' <param name="ColumnMapObject">The column mapping object attached to this object (usually cmapObjectName) used for
    ''' columns where the column name is different from the property name</param>
    Private Sub MapData(ByRef ObjectToMapDataTo As Object, ByRef DataRowToMapDataFrom As DataRow, ByRef DataColumnsForNames As DataColumnCollection, ByRef ColumnMapObject As ColumnMapping)
        IsMappingData = True

        Dim curProperty As String
        Dim curColumn As DataColumn
        Dim PropInfo As PropertyInfo

        For Each curColumn In DataColumnsForNames
            curProperty = GetColumnMap(ColumnMapObject, curColumn.ColumnName)
            Try
                PropInfo = ObjectToMapDataTo.GetType.GetProperty(curProperty)
                If Not PropInfo Is Nothing And Not DataRowToMapDataFrom.Item(curColumn) Is DBNull.Value Then
                    PropInfo.SetValue(ObjectToMapDataTo, DataRowToMapDataFrom.Item(curColumn), Nothing)
                    'System.Console.WriteLine(PropInfo.Name + " | " + curRow.Item(curColumn).ToString)
                End If
            Catch ex As Exception
                '<GenericError> Modify as needed
                Throw New Exception("There was an error mapping the data: " + curColumn.ColumnName, ex)
            End Try
        Next

        IsMappingData = False

    End Sub

    ''' <summary>
    ''' This is the reverse of the MapData, it will take the properties of a dbiWSFObject and map them to a data row
    ''' provided the column names match the property name or have been set in the column mapping.
    ''' </summary>
    ''' <remarks>As with the MapData method, if you need to you can recode this to suit your needs</remarks>
    ''' <param name="ObjectToMapPropertiesFrom">The dbiWSF Object whose properties you want to send to the data row</param>
    ''' <param name="DataRowToMapPropertiesTo">The data row that will have it's items updated from the properties</param>
    ''' <param name="DataColumnsForNames">A collection of data columns (usually datatable.columns) used in the
    ''' reflection to get the property names</param>
    ''' <param name="ColumnMapObject">The column mapping object attached to this object (usually cmapObjectName) used for
    ''' columns where the column name is different from the property name</param>
    Private Sub ObjectPropertyToDatarow(ByRef ObjectToMapPropertiesFrom As Object, ByRef DataRowToMapPropertiesTo As DataRow, ByRef DataColumnsForNames As DataColumnCollection, ByRef ColumnMapObject As ColumnMapping)
        If Not IsMappingData And Not IsLoadingData Then

            Dim curProperty As String

            Dim curColumn As DataColumn
            Dim propInfo As PropertyInfo

            For Each curColumn In DataColumnsForNames
                curProperty = GetColumnMap(ColumnMapObject, curColumn.ColumnName)
                Try
                    propInfo = ObjectToMapPropertiesFrom.GetType.GetProperty(curProperty)

                    Select Case propInfo.PropertyType.ToString

                        Case GetType(System.DateTime).ToString
                            'System.Console.WriteLine(PropInfo.Name + " | " + curRow.Item(curColumn).ToString)
                            If propInfo.GetValue(ObjectToMapPropertiesFrom, Nothing) Is Nothing Or propInfo.GetValue(ObjectToMapPropertiesFrom, Nothing) = Date.MinValue Then
                                DataRowToMapPropertiesTo.Item(curColumn) = DBNull.Value
                            Else
                                DataRowToMapPropertiesTo.Item(curColumn) = propInfo.GetValue(ObjectToMapPropertiesFrom, Nothing)
                            End If
                        Case Else
                            'System.Console.WriteLine(PropInfo.Name + " | " + curRow.Item(curColumn).ToString)
                            If propInfo.GetValue(ObjectToMapPropertiesFrom, Nothing) Is Nothing Then
                                DataRowToMapPropertiesTo.Item(curColumn) = DBNull.Value
                            Else
                                DataRowToMapPropertiesTo.Item(curColumn) = propInfo.GetValue(ObjectToMapPropertiesFrom, Nothing)
                            End If
                    End Select
                Catch ex As Exception
                    '<GenericError> Modify as needed
                    Throw New Exception("There was an error setting the data row from the properties: " + curColumn.ColumnName, ex)
                End Try
            Next
        End If
    End Sub

    ''' <summary>This is an internal function used to test for data table changes</summary>
    ''' <param name="DataTableToTest">The data table to check for changes</param>
    Private Function GetTableChanges(ByVal DataTableToTest As DataTable) As Boolean
        Dim TableToTest As DataTable = DataTableToTest.GetChanges
        If TableToTest Is Nothing Then
            Return False
        Else
            Return True
        End If
    End Function

#End Region

#Region "**Supporting Classes**"
    ''' <summary>
    ''' Used for mapping data table column names to dbiWSFObject.Properties
    ''' </summary>
    ''' <remarks>By default the Framework is designed to have database columns reflect the names of the properties 
    ''' on the objects. As some data tiers exist outside of the developers control we added this class to be able
    ''' to map column names that are different to the object properties they represent 
    '''</remarks>
    Public Class ColumnMapping
        Dim l_Mapping As New System.Collections.Generic.Dictionary(Of String, String)
        ''' <summary>A dictionary of key value pairs.</summary>
        ''' <remarks>The first string is the database column name with the second 
        ''' string representing the property name it is associated with. 
        ''' The Framework data binding will call the <see cref="dbiWSF.Databinding.GetColumnMap"></see> method
        ''' to find any mappings before binding to the data. If there is no matching key for the column/property then
        ''' the column name is used (meaning that you only have to add mappings for column names that don't match
        ''' the properties.</remarks>
        Public Property Mapping() As System.Collections.Generic.Dictionary(Of String, String)
            Get
                Return l_Mapping
            End Get
            Set(ByVal value As System.Collections.Generic.Dictionary(Of String, String))
                l_Mapping = value
            End Set
        End Property
    End Class

#End Region

#Region "**Public LoadData Subroutines**"
    ''' <summary>Load the cargo types</summary>
    ''' <returns>A sorted list collection of the cargo type description(string) and the cargo type object</returns>
    Public Function LoadCargoTypes() As System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)
        Dim retCargo As New System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)
        Select Case Me.ConnectionType
            Case ConnectionTypes.SQLDatabase
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retCargo = LoadCargoSQL()
                Else
                    LoadCargoCustomizedSQL()
                End If
            Case ConnectionTypes.OLEDatabase
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retCargo = LoadCargoOLE()
                Else
                    LoadCargoCustomizedOLE()
                End If
            Case Else
                'Default to SQL Connection type if me.ConnectionType is not SQL/OLE
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retCargo = LoadCargoSQL()
                Else
                    LoadCargoCustomizedSQL()
                End If
        End Select
        Return retCargo
    End Function

    ''' <summary>Load the loading dock exceptions</summary>
    ''' <returns>A collection of loading dock exceptions</returns>
    Public Function LoadLoadingDockExceptions() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)
        Dim retExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)
        Select Case Me.ConnectionType
            Case ConnectionTypes.SQLDatabase
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retExceptions = LoadLoadingDockExceptionsSQL()
                Else
                    LoadLoadingDockExceptionsCustomizedSQL()
                End If
            Case ConnectionTypes.OLEDatabase
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retExceptions = LoadLoadingDockExceptionsOLE()
                Else
                    LoadLoadingDockExceptionsCustomizedOLE()
                End If
            Case Else
                'Default to SQL Connection type if me.ConnectionType is not SQL/OLE
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retExceptions = LoadLoadingDockExceptionsSQL()
                Else
                    LoadLoadingDockExceptionsCustomizedSQL()
                End If
        End Select
        Return retExceptions
    End Function

    ''' <summary>Load the recurring exceptions</summary>
    ''' <returns>List collection of recurrence objects</returns>
    Public Function LoadRecurrenceExceptions() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)
        Dim retRecurringExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)
        Select Case Me.ConnectionType
            Case ConnectionTypes.SQLDatabase
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retRecurringExceptions = LoadRecurringExceptionsSQL()
                Else
                    LoadRecurringExceptionsCustomizedSQL()
                End If
            Case ConnectionTypes.OLEDatabase
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retRecurringExceptions = LoadRecurringExceptionsOLE()
                Else
                    LoadRecurringExceptionsCustomizedOLE()
                End If
            Case Else
                'Default to SQL Connection type if me.ConnectionType is not SQL/OLE
                If Me.DataBindingMethod <> DataBindingMethods.Customized Then
                    retRecurringExceptions = LoadRecurringExceptionsSQL()
                Else
                    LoadRecurringExceptionsCustomizedSQL()
                End If
        End Select
        Return retRecurringExceptions
    End Function

#End Region

#Region "**SQL Databinding Methods**"

#Region "--SQL Supporting Databinding--"

    ''' <summary>Supporting Method: Load the cargo types using std/inmemory SQL</summary>
    ''' <returns>A sorted list collection of the cargo type description(string) and the cargo type object</returns>
    Private Function LoadCargoSQL() As System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)
        Dim collCargoTypes As New System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)

        Try
            datatableCargoTypes.Clear()
            dataadapterCargoTypes = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objCargoType)) _
                                                        + " ORDER BY Description", Me.ConnectionString)
            Dim dcbCargoType As New SqlClient.SqlCommandBuilder(dataadapterCargoTypes)

            dataadapterCargoTypes.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterCargoTypes.Fill(datatableCargoTypes)

            For Each curRow As DataRow In datatableCargoTypes.Rows
                Dim newCargoType As New dbiWSF.FrameworkObjects.objCargoType(Me.WSFObjects)
                newCargoType.CargoID = curRow("CargoID")
                newCargoType.Description = curRow("Description")
                newCargoType.Notes = curRow("Notes")
                newCargoType.Icon = curRow("Icon")
                newCargoType.AttachedDataRow = curRow
                collCargoTypes.Add(newCargoType.Description, newCargoType)
            Next

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading CargoTypes", ex)
        End Try
        Return collCargoTypes
    End Function

    ''' <summary>Supporting Method: Load the cargo types using CustomizedSQL</summary>
    ''' <returns>A sorted list collection of the cargo type description(string) and the cargo type object</returns>
    Private Function LoadCargoCustomizedSQL() As System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)
        Dim collCargoTypes As New System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)

        Try
            'Load cargo types here
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading CargoTypes", ex)
        End Try
        Return collCargoTypes
    End Function

    ''' <summary>Supporting Method: Load the loading dock exceptions using std/inmemory SQL</summary>
    ''' <returns>A collection of loading dock exceptions</returns>
    Private Function LoadLoadingDockExceptionsSQL() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Dim collExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Try
            datatableLoadingDockExceptions.Clear()
            dataadapterLoadingDockExceptions = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDockException)) _
                                                                                 , Me.ConnectionString)
            Dim dcbLoadingDockExceptions As New System.Data.SqlClient.SqlCommandBuilder(dataadapterLoadingDockExceptions)

            dataadapterLoadingDockExceptions.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterLoadingDockExceptions.Fill(datatableLoadingDockExceptions)

            datatableExceptionDockKeys.Clear()
            dataadapterExceptionDockKeys = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM ExceptionDockKeys", Me.ConnectionString)
            Dim dcbExceptionDockKey As New System.Data.SqlClient.SqlCommandBuilder(dataadapterExceptionDockKeys)

            dataadapterExceptionDockKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterExceptionDockKeys.Fill(datatableExceptionDockKeys)


            For Each curRow As DataRow In datatableLoadingDockExceptions.Rows
                Dim newException As New dbiWSF.FrameworkObjects.objLoadingDockException(Me.WSFObjects)
                MapData(newException, curRow, datatableLoadingDockExceptions.Columns, cmapLoadingDockExceptions)
                Dim collDockIDs = From curDataRow As DataRow In datatableExceptionDockKeys.Rows _
                                   Where curDataRow("LoadingDockExceptionID") = newException.LoadingDockExceptionID _
                                   Select curDataRow("LoadingDockID")

                For Each dockID As System.Guid In collDockIDs
                    newException.LoadingDocks.Add(dockID)
                Next
                newException.AttachedDataRow = curRow
                collExceptions.Add(newException)
            Next

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Exceptions", ex)
        End Try

        Return collExceptions
    End Function

    ''' <summary>Supporting Method: Load the loading dock exceptions using CustomizedSQL</summary>
    ''' <returns>A collection of loading dock exceptions</returns>
    Private Function LoadLoadingDockExceptionsCustomizedSQL() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Dim collExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Try
            'Load loading dock exceptions

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Exceptions", ex)
        End Try

        Return collExceptions
    End Function

    ''' <summary>Supporting Method: Load the recurring loading dock exceptions using std/inmemory SQL</summary>
    ''' <returns>List collection of recurrence objects</returns>
    Private Function LoadRecurringExceptionsSQL() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)

        Dim collRecurringExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)


        Try
            datatableRecurrences.Clear()
            dataadapterRecurrences = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objRecurrence)) _
                                                                         + " WHERE ObjectType = 0" _
                                                                         , Me.ConnectionString)
            Dim dcbRecurrences As New System.Data.SqlClient.SqlCommandBuilder(dataadapterRecurrences)
            dataadapterRecurrences.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterRecurrences.Fill(datatableRecurrences)

            datatableRecurrenceObjectKeys.Clear()
            dataadapterRecurrenceObjectKeys = New System.Data.SqlClient.SqlDataAdapter("SELECT * FROM RecurrenceObjectKeys", Me.ConnectionString)
            Dim dcbRecurrenceObjectKeys As New System.Data.SqlClient.SqlCommandBuilder(dataadapterRecurrenceObjectKeys)
            dataadapterRecurrenceObjectKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterRecurrenceObjectKeys.Fill(datatableRecurrenceObjectKeys)


            For Each curRow As DataRow In datatableRecurrences.Rows
                Dim newRecurrence As New dbiWSF.FrameworkObjects.objRecurrence(Me.WSFObjects)
                MapData(newRecurrence, curRow, datatableRecurrences.Columns, cmapRecurrences)
                Dim collDockIDs = From curDataRow As DataRow In datatableRecurrenceObjectKeys.Rows _
                                   Where curDataRow("RecurrenceID") = newRecurrence.RecurrenceID _
                                   Select curDataRow("ObjectID")

                For Each dockID As System.Guid In collDockIDs
                    newRecurrence.RecurringObjects.Add(dockID)
                Next

                newRecurrence.AttachedDataRow = curRow
                collRecurringExceptions.Add(newRecurrence)

            Next

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Recurrence", ex)
        End Try
        Return collRecurringExceptions
    End Function

    ''' <summary>Supporting Method: Load the recurring loading dock exceptions using CustomizedSQL</summary>
    ''' <returns>List collection of recurrence objects</returns>
    Private Function LoadRecurringExceptionsCustomizedSQL() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)

        Dim collRecurringExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)
        Try
            'Load recurring exceptions
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Recurrence", ex)
        End Try
        Return collRecurringExceptions
    End Function

    ''' <summary>
    ''' Supporting Method: Loads unassigned order headers into the OrderHeader datatable
    ''' </summary>
    ''' <remarks>Because the OrderHeaders are loaded by Shipment, those Order Headers that don't
    ''' belong to a shipment aren't loaded when the main dataloads occur. This method will compensate for that.</remarks>
    Private Sub LoadUnassignedOrderHeadersSQL()
        Dim daLoadUnassignedOH As SqlClient.SqlDataAdapter
        Dim daLoadUnassignedOD As SqlClient.SqlDataAdapter

        Dim strOrderHeaderTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader))
        Dim strOrderDetailTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail))


        Dim strOrderHeaderSelect As String = "SELECT " + strOrderHeaderTableName + ".* FROM " + strOrderHeaderTableName + _
        " WHERE " + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = '" + System.Guid.Empty.ToString + "'" + _
        " ORDER BY " + GetColumnMap(cmapOrderHeaders, "OrderEntryID")

        daLoadUnassignedOH = New SqlClient.SqlDataAdapter(strOrderHeaderSelect, Me.ConnectionString)
        daLoadUnassignedOH.MissingSchemaAction = MissingSchemaAction.AddWithKey

        daLoadUnassignedOH.Fill(datatableOrderHeaders)

        Dim strOrderDetailSelect As String = "SELECT " + strOrderDetailTableName + ".* FROM (" + strOrderDetailTableName + _
               " INNER JOIN " + strOrderHeaderTableName + _
               " ON " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "OrderID") + " = " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "OrderID") + ")" + _
               " WHERE " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = '" + System.Guid.Empty.ToString + "'" + _
               " ORDER BY " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "LineNumber")

        daLoadUnassignedOD = New SqlClient.SqlDataAdapter(strOrderDetailSelect, Me.ConnectionString)
        daLoadUnassignedOD.MissingSchemaAction = MissingSchemaAction.AddWithKey

        daLoadUnassignedOD.Fill(datatableOrderDetails)
    End Sub

    ''' <summary>Supporting Method: Add the insert, update and delete commands
    ''' to the SQLdataadapter for Order Headers</summary>
    Private Sub dcbSQLOrderHeader()
        Dim orderIDMap As String = GetColumnMap(cmapOrderHeaders, "OrderID")
        Try

            Dim datacommandUpdate As New SqlClient.SqlCommand()

            Dim sqlOHUpdate As String
            sqlOHUpdate = "Update [" + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) + "] SET "

            For Each DataColumnToAdd As DataColumn In datatableOrderHeaders.Columns
                sqlOHUpdate += "[" + DataColumnToAdd.ColumnName + "] = @" + DataColumnToAdd.ColumnName + ", "
                Dim newParameter As New SqlClient.SqlParameter()
                newParameter.DbType = GetSQLDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.InputOutput
                newParameter.ParameterName = "@" + DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                datacommandUpdate.Parameters.Add(newParameter)
            Next

            sqlOHUpdate = sqlOHUpdate.Remove(sqlOHUpdate.Length - 2)

            sqlOHUpdate += " WHERE (" + orderIDMap + " = @" + orderIDMap + ")"
            datacommandUpdate.CommandText = sqlOHUpdate
            datacommandUpdate.Connection = New SqlClient.SqlConnection(Me.ConnectionString)
            Me.dataadapterOrderHeaders.UpdateCommand = datacommandUpdate

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Update Command to dataadapterOrderHeader", ex)
        End Try


        Try
            Dim datacommandInsert As New SqlClient.SqlCommand

            Dim sqlOHInsert As String
            Dim sqlValueString As String = ""
            sqlOHInsert = "INSERT INTO " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) + " ("
            For Each DataColumnToAdd As DataColumn In datatableOrderHeaders.Columns
                sqlOHInsert += DataColumnToAdd.ColumnName + ", "
                sqlValueString += "@" + DataColumnToAdd.ColumnName + ", "

                Dim newParameter As New SqlClient.SqlParameter()
                newParameter.DbType = GetSQLDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.InputOutput
                newParameter.ParameterName = "@" + DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                datacommandInsert.Parameters.Add(newParameter)
            Next

            sqlOHInsert = sqlOHInsert.Remove(sqlOHInsert.Length - 2)
            sqlValueString = sqlValueString.Remove(sqlValueString.Length - 2)
            sqlOHInsert += ") VALUES(" + sqlValueString + ")"
            datacommandInsert.CommandText = sqlOHInsert
            datacommandInsert.Connection = New SqlClient.SqlConnection(Me.ConnectionString)
            Me.dataadapterOrderHeaders.InsertCommand = datacommandInsert

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Insert Command to dataadapterOrderHeader", ex)
        End Try

        Try
            Dim datacommandDelete As New SqlClient.SqlCommand
            Dim newParameter As New SqlClient.SqlParameter("@" + orderIDMap, SqlDbType.UniqueIdentifier, 0, "OrderID")
            datacommandDelete.Parameters.Add(newParameter)
            datacommandDelete.CommandText = "DELETE FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) + " WHERE (" + orderIDMap + " = @" + orderIDMap + ")"
            datacommandDelete.Connection = New SqlClient.SqlConnection(Me.ConnectionString)
            Me.dataadapterOrderHeaders.DeleteCommand = datacommandDelete
        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Delete Command to dataadapterOrderHeader", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Add the insert, update and delete commands
    ''' to the SQLdataadapter for Order Details</summary>
    Private Sub dcbSQLOrderDetail()
        Dim orderIDMap As String = GetColumnMap(cmapOrderDetails, "OrderDetailID")
        Try

            Dim datacommandUpdate As New SqlClient.SqlCommand()

            Dim sqlOHUpdate As String
            sqlOHUpdate = "Update [" + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) + "] SET "

            For Each DataColumnToAdd As DataColumn In datatableOrderDetails.Columns
                sqlOHUpdate += "[" + DataColumnToAdd.ColumnName + "] = @" + DataColumnToAdd.ColumnName + ", "
                Dim newParameter As New SqlClient.SqlParameter()
                newParameter.DbType = GetSQLDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.InputOutput
                newParameter.ParameterName = "@" + DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                datacommandUpdate.Parameters.Add(newParameter)
            Next

            sqlOHUpdate = sqlOHUpdate.Remove(sqlOHUpdate.Length - 2)

            sqlOHUpdate += " WHERE (" + orderIDMap + " = @" + orderIDMap + ")"
            datacommandUpdate.CommandText = sqlOHUpdate
            datacommandUpdate.Connection = New SqlClient.SqlConnection(Me.ConnectionString)
            Me.dataadapterOrderDetails.UpdateCommand = datacommandUpdate

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Update Command to dataadapterOrderDetail", ex)
        End Try


        Try
            Dim datacommandInsert As New SqlClient.SqlCommand

            Dim sqlOHInsert As String
            Dim sqlValueString As String = ""
            sqlOHInsert = "INSERT INTO " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) + " ("
            For Each DataColumnToAdd As DataColumn In datatableOrderDetails.Columns
                sqlOHInsert += DataColumnToAdd.ColumnName + ", "
                sqlValueString += "@" + DataColumnToAdd.ColumnName + ", "

                Dim newParameter As New SqlClient.SqlParameter()
                newParameter.DbType = GetSQLDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.InputOutput
                newParameter.ParameterName = "@" + DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                datacommandInsert.Parameters.Add(newParameter)
            Next

            sqlOHInsert = sqlOHInsert.Remove(sqlOHInsert.Length - 2)
            sqlValueString = sqlValueString.Remove(sqlValueString.Length - 2)
            sqlOHInsert += ") VALUES(" + sqlValueString + ")"
            datacommandInsert.CommandText = sqlOHInsert
            datacommandInsert.Connection = New SqlClient.SqlConnection(Me.ConnectionString)
            Me.dataadapterOrderDetails.InsertCommand = datacommandInsert

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Insert Command to dataadapterOrderDetail", ex)
        End Try

        Try
            Dim datacommandDelete As New SqlClient.SqlCommand
            Dim newParameter As New SqlClient.SqlParameter("@" + orderIDMap, SqlDbType.UniqueIdentifier, 0, orderIDMap)
            datacommandDelete.Parameters.Add(newParameter)
            datacommandDelete.CommandText = "DELETE FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) + " WHERE (" + orderIDMap + " = @" + orderIDMap + ")"
            datacommandDelete.Connection = New SqlClient.SqlConnection(Me.ConnectionString)
            Me.dataadapterOrderDetails.DeleteCommand = datacommandDelete
        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Delete Command to dataadapterOrderDetail", ex)
        End Try
    End Sub

    ''' <summary>Gets the SQLDBType from a base type</summary>
    ''' <param name="TypeToConvert">The base system type to convert to DbType</param>
    Private Function GetSQLDBType(ByVal TypeToConvert As System.Type) As DbType
        Try

            Dim p1 As SqlClient.SqlParameter
            Dim tc As System.ComponentModel.TypeConverter
            p1 = New SqlClient.SqlParameter()
            tc = System.ComponentModel.TypeDescriptor.GetConverter(p1.DbType)
            If tc.CanConvertFrom(TypeToConvert) Then
                p1.DbType = tc.ConvertFrom(TypeToConvert.Name)
            Else
                Try
                    p1.DbType = tc.ConvertFrom(TypeToConvert.Name)
                Catch ex As Exception
                    p1.DbType = DbType.String
                End Try
            End If
            Return p1.DbType

        Catch ex As Exception
            Return DbType.String
        End Try

    End Function

#End Region

#Region "--Standard Databinding--"

    ''' <summary>Supporting Method: Load the Framework data using SQL and
    ''' <see cref="dbiWSF.Databinding.DataBindingMethods"> Standard data binding</see></summary>
    ''' <remarks>Standard data binding loads the tables in the order they are needed to generate the objects. This
    ''' classed loading can take some time as there is no database optimization added. We've gone with a flat
    ''' table structure to ease the update process, however this can be quite slow. We recommend using InMemory data binding or 
    ''' if you need even more flexibility use the Customized data model.</remarks>
    ''' <returns>A list collection of warehouse objects</returns>
    Private Function LoadStandardSQL() As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Try


            datatableWarehouses.Clear()
            dataadapterWarehouses = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objWarehouse)) _
                                                        + " ORDER BY Description", Me.ConnectionString)
            Dim dcbWarehouses As New SqlClient.SqlCommandBuilder(dataadapterWarehouses)

            dataadapterWarehouses.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterWarehouses.Fill(datatableWarehouses)


            For Each curRow As DataRow In datatableWarehouses.Rows
                Dim newWarehouse As New dbiWSF.FrameworkObjects.objWarehouse(Me.WSFObjects)
                MapData(newWarehouse, curRow, datatableWarehouses.Columns, cmapWarehouses)
                newWarehouse.AttachedDataRow = curRow
                LoadStandardLoadingDocksSQL(newWarehouse)
                retWarehouses.Add(newWarehouse)
            Next
            Try
                LoadStandardUnassignedShipmentsSQL()
            Catch ex As Exception
                Throw New Exception("Error in LoadUnassignedShipments", ex) ' <GenericError> replace as needed
            End Try

        Catch ex As Exception
            Throw New Exception("Error in Warehouse SQL Select Statement", ex) ' <GenericError> replace as needed
        End Try
        Return retWarehouses
    End Function

    ''' <summary>Supporting Method: Used for SQL STD</summary>
    ''' <param name="Warehouse">The warehouse object to load the loading docks for</param>
    Private Sub LoadStandardLoadingDocksSQL(ByRef Warehouse As dbiWSF.FrameworkObjects.objWarehouse)
        Dim curWarehouseID As System.Guid = Warehouse.WarehouseID

        Try
            dataadapterLoadingDocks = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDock)) _
                                                   + " WHERE " + GetColumnMap(cmapLoadingDocks, "WarehouseID") + " = '" + Warehouse.WarehouseID.ToString _
                                                   + "' ORDER BY WarehouseOrdinal", Me.ConnectionString)
            Dim dcbDocks As New SqlClient.SqlCommandBuilder(dataadapterLoadingDocks)

            dataadapterLoadingDocks.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterLoadingDocks.Fill(datatableLoadingDocks)

            Dim colDocks = From selectItems As DataRow In datatableLoadingDocks.Rows _
                           Where selectItems.Item(GetColumnMap(cmapLoadingDocks, "WarehouseID")) = curWarehouseID _
                           Select selectItems


            For Each curRow As DataRow In colDocks
                Dim newDock As New dbiWSF.FrameworkObjects.objLoadingDock(Me.WSFObjects)
                MapData(newDock, curRow, datatableLoadingDocks.Columns, cmapLoadingDocks)
                newDock.AttachedDataRow = curRow

                Try
                    dataadapterLoadingDockCargoKeys = New SqlClient.SqlDataAdapter("SELECT * FROM LoadingDockCargoKeys" + _
                                                                          " WHERE LoadingDockID = '" + newDock.LoadingDockID.ToString + _
                                                                          "'", Me.ConnectionString)
                    Dim dcbLoadingDockCargoKeys As New SqlClient.SqlCommandBuilder(dataadapterLoadingDockCargoKeys)

                    dataadapterLoadingDockCargoKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
                    dataadapterLoadingDockCargoKeys.Fill(datatableLoadingDockCargoKeys)

                    Dim datarowCargoTypes() As DataRow = datatableLoadingDockCargoKeys.Select("LoadingDockID = '" + newDock.LoadingDockID.ToString + "'")

                    For Each datarowCargoType As DataRow In datarowCargoTypes
                        newDock.CargoTypes.Add(datarowCargoType("CargoTypeID"))
                    Next
                Catch ex As Exception
                    '<GenericError> Replace as needed.
                    Throw New Exception("Error in Loading Dock Cargo Types Load", ex)
                End Try


                LoadStandardShipmentsSQL(newDock)
                Warehouse.LoadingDocks.Add(newDock)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Dock Load", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for SQL STD</summary>
    Private Sub LoadStandardShipmentsSQL(ByRef LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock)
        Dim curLoadingDockID As System.Guid = LoadingDock.LoadingDockID

        Try
            dataadapterShipments = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment)) _
                                                       + " WHERE " + GetColumnMap(cmapShipments, "LoadingDockID") _
                                                       + " = '" + LoadingDock.LoadingDockID.ToString + "' ORDER BY SchedStartTime", Me.ConnectionString)
            Dim dcbShipments As New SqlClient.SqlCommandBuilder(dataadapterShipments)

            dataadapterShipments.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterShipments.Fill(datatableShipments)

            Dim colShipments = From selectItems As DataRow In datatableShipments.Rows _
               Where selectItems.Item(GetColumnMap(cmapShipments, "LoadingDockID")) = curLoadingDockID _
               Select selectItems


            For Each curRow As DataRow In colShipments
                Dim newShipment As New dbiWSF.FrameworkObjects.objShipment(Me.WSFObjects)
                newShipment.AttachedDataRow = curRow
                MapData(newShipment, curRow, datatableShipments.Columns, cmapShipments)
                LoadStandardOrderHeaderSQL(newShipment)
                LoadingDock.Shipments.Add(newShipment)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Shipments Standard SQL", ex)
        End Try

    End Sub

    ''' <summary>Supporting Method: Used for SQL STD</summary>
    Private Sub LoadStandardOrderHeaderSQL(ByRef Shipment As dbiWSF.FrameworkObjects.objShipment)
        Dim curShipmentID As System.Guid = Shipment.ShipmentID

        Try
            dataadapterOrderHeaders = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) _
                                                         + " WHERE " + GetColumnMap(cmapOrderHeaders, "ShipmentID") _
                                                         + " = '" + Shipment.ShipmentID.ToString + "' ORDER BY OrderEntryID", Me.ConnectionString)
            Dim dcbOrderHeaders As New SqlClient.SqlCommandBuilder(dataadapterOrderHeaders)
            dataadapterOrderHeaders.MissingSchemaAction = MissingSchemaAction.AddWithKey

            dataadapterOrderHeaders.Fill(datatableOrderHeaders)

            Dim colOrderHeaders = From selectItems As DataRow In datatableOrderHeaders.Rows _
              Where selectItems.Item(GetColumnMap(cmapOrderHeaders, "ShipmentID")) = curShipmentID _
              Select selectItems

            For Each curRow As DataRow In colOrderHeaders
                Dim newOrderHeader As New dbiWSF.FrameworkObjects.objOrderHeader(Me.WSFObjects)
                newOrderHeader.AttachedDataRow = curRow
                MapData(newOrderHeader, curRow, datatableOrderHeaders.Columns, cmapOrderHeaders)
                LoadStandardOrderDetailSQL(newOrderHeader)
                Shipment.OrderHeaders.Add(newOrderHeader)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Order Headers Standard SQL", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for SQL STD</summary>
    Private Sub LoadStandardOrderDetailSQL(ByRef OrderHeader As dbiWSF.FrameworkObjects.objOrderHeader)
        Dim curOrderHeaderID As System.Guid = OrderHeader.OrderID

        Try
            dataadapterOrderDetails = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) _
                                                         + " WHERE " + GetColumnMap(cmapOrderDetails, "OrderID") _
                                                         + " = '" + OrderHeader.OrderID.ToString + "' ORDER BY LineNumber", Me.ConnectionString)
            Dim dcbOrderDetails As New SqlClient.SqlCommandBuilder(dataadapterOrderDetails)
            dataadapterOrderDetails.MissingSchemaAction = MissingSchemaAction.AddWithKey

            dataadapterOrderDetails.Fill(datatableOrderDetails)

            Dim colOrderDetails = From selectItems As DataRow In datatableOrderDetails.Rows _
              Where selectItems.Item(GetColumnMap(cmapOrderDetails, "OrderID")) = curOrderHeaderID _
              Select selectItems

            For Each curRow As DataRow In colOrderDetails
                Dim newOrderDetail As New dbiWSF.FrameworkObjects.objOrderDetail(Me.WSFObjects)
                newOrderDetail.AttachedDataRow = curRow
                MapData(newOrderDetail, curRow, datatableOrderDetails.Columns, cmapOrderDetails)
                OrderHeader.OrderDetails.Add(newOrderDetail)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Order Details Standard SQL", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for SQL STD</summary>
    Private Sub LoadStandardUnassignedShipmentsSQL()
        Try
            dataadapterShipments = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment)) _
                                                       + " WHERE " + GetColumnMap(cmapShipments, "LoadingDockID") _
                                                       + " IS NULL ORDER BY SchedStartTime", Me.ConnectionString)
            Dim dcbShipments As New SqlClient.SqlCommandBuilder(dataadapterShipments)

            dataadapterShipments.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterShipments.Fill(datatableShipments)

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Unassigned ", ex)
        End Try
    End Sub

#End Region

#Region "--Customized Databinding--"
    ''' <summary>Supporting Method: Load the Framework data using SQL and
    ''' <see cref="dbiWSF.Databinding.DataBindingMethods"> Customized data binding</see>
    ''' </summary>
    ''' <remarks>Note: by default there is no code in this function, this is a placeholder for custom SQL
    ''' Statements to connect to an SQL database. It is up to the developer to create the CRUD for said code and
    ''' to ensure it flows through the rest of the data binding class. This is where you would sync the data tables
    ''' to stored procedures or custom views/queries to the databases.</remarks>
    ''' <returns>A list collection of warehouse objects</returns>
    Private Function LoadCustomizedSQL() As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        'Ensure that you populate the data tables as necessary
        'Also be sure to modify the Update, Insert and Delete commands on the
        'appropriate data adapters
        dataadapterWarehouses.UpdateCommand.CommandText = ""
        dataadapterWarehouses.InsertCommand.CommandText = ""
        dataadapterWarehouses.DeleteCommand.CommandText = ""

        dataadapterLoadingDocks.UpdateCommand.CommandText = ""
        dataadapterLoadingDocks.InsertCommand.CommandText = ""
        dataadapterLoadingDocks.DeleteCommand.CommandText = ""

        dataadapterShipments.UpdateCommand.CommandText = ""
        dataadapterShipments.InsertCommand.CommandText = ""
        dataadapterShipments.DeleteCommand.CommandText = ""

        Return retWarehouses
    End Function



#End Region

#Region "--InMemory Databinding--"
    ''' <summary>Supporting Method: Load the Framework data using SQL and
    ''' <see cref="Databinding.DataBindingMethods"> InMemory data binding</see>
    ''' </summary>
    ''' <remarks>InMemory data binding loads all the data requested into memory first then uses
    ''' LINQ to parse out the data to the objects. This avoids multiple JOIN statements and custom
    ''' coding which greatly improves performance. If you need more control use the Customized data binding model.</remarks>
    ''' <returns>A list collection of warehouse objects</returns>
    ''' <param name="StartDate">The start date/time for the data being loaded.</param>
    ''' <param name="EndDate">The end date/time for the data being loaded.</param>
    Private Function LoadInMemorySQL(ByVal StartDate As Date, ByVal EndDate As Date) As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)

        Try
            datatableWarehouses.Clear()
            dataadapterWarehouses = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objWarehouse)) _
                                                        + " ORDER BY Description", Me.ConnectionString)
            Dim dcbWarehouses As New SqlClient.SqlCommandBuilder(dataadapterWarehouses)

            dataadapterWarehouses.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterWarehouses.Fill(datatableWarehouses)

            datatableLoadingDocks.Clear()

            Try
                dataadapterLoadingDocks = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDock)) _
                                                       + " ORDER BY WarehouseOrdinal", Me.ConnectionString)
                Dim dcbDocks As New SqlClient.SqlCommandBuilder(dataadapterLoadingDocks)

                dataadapterLoadingDocks.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterLoadingDocks.Fill(datatableLoadingDocks)

            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Dock Load", ex)
            End Try

            Try
                dataadapterLoadingDockExceptions = New SqlClient.SqlDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDockException)) _
                                                       + " ORDER BY StartDate", Me.ConnectionString)

                Dim dcbLoadingDockExceptions As New SqlClient.SqlCommandBuilder(dataadapterLoadingDockExceptions)

                dataadapterLoadingDockExceptions.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterLoadingDockExceptions.Fill(datatableLoadingDockExceptions)

            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Dock Exception Load", ex)
            End Try


            Try
                dataadapterLoadingDockCargoKeys = New SqlClient.SqlDataAdapter("SELECT * FROM LoadingDockCargoKeys", Me.ConnectionString)
                Dim dcbLoadingDockCargoKeys As New SqlClient.SqlCommandBuilder(dataadapterLoadingDockCargoKeys)

                dataadapterLoadingDockCargoKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterLoadingDockCargoKeys.Fill(datatableLoadingDockCargoKeys)
            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Dock Cargo Types Load", ex)
            End Try

            Try
                Dim strShipmentSelect As String = "SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment)) + _
                " WHERE ((" + GetColumnMap(cmapShipments, "SchedStartTime") + " BETWEEN '" & StartDate.ToString & "' AND '" & EndDate.ToString & "')" + _
                " OR (" + GetColumnMap(cmapShipments, "SchedEndTime") + " BETWEEN '" & StartDate.ToString & "' AND '" & EndDate.ToString & "'))" + _
                " ORDER BY " + GetColumnMap(cmapShipments, "SchedStartTime")

                dataadapterShipments = New SqlClient.SqlDataAdapter(strShipmentSelect, Me.ConnectionString)
                Dim dcbShipments As New SqlClient.SqlCommandBuilder(dataadapterShipments)

                dataadapterShipments.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterShipments.Fill(datatableShipments)

            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Shipments", ex)
            End Try

            Try
                Dim strOrderHeaderTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader))
                Dim strShipmentTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment))

                Dim strOrderHeaderSelect As String = "SELECT " + strOrderHeaderTableName + ".* FROM " + strOrderHeaderTableName + _
                " INNER JOIN " + strShipmentTableName + _
                " ON " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = " + strShipmentTableName + "." + GetColumnMap(cmapShipments, "ShipmentID") + _
                " WHERE ((" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedStartTime") + _
                " BETWEEN '" + StartDate.ToString + "' AND '" + EndDate.ToString + "')" + _
                " OR (" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedEndTime") + _
                " BETWEEN '" + StartDate.ToString + "' AND '" + EndDate.ToString + "'))" + _
                " ORDER BY " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID")

                dataadapterOrderHeaders = New SqlClient.SqlDataAdapter(strOrderHeaderSelect, Me.ConnectionString)

                dataadapterOrderHeaders.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterOrderHeaders.Fill(datatableOrderHeaders)

                'We need to use a custom command builder here because of the complex joins for date selection
                dcbSQLOrderHeader()


            Catch ex As Exception

            End Try

            Try
                Dim strOrderHeaderTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader))
                Dim strOrderDetailTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail))
                Dim strShipmentTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment))

                Dim strOrderDetailSelect As String = "SELECT " + strOrderDetailTableName + ".* FROM " + strOrderDetailTableName + _
                " INNER JOIN " + strOrderHeaderTableName + _
                " ON " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "OrderID") + " = " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "OrderID") + _
                " INNER JOIN " + strShipmentTableName + _
                " ON " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = " + strShipmentTableName + "." + GetColumnMap(cmapShipments, "ShipmentID") + _
                " WHERE ((" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedStartTime") + _
                " BETWEEN '" + StartDate.ToString + "' AND '" + EndDate.ToString + "') " + _
                " OR (" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedEndTime") + _
                " BETWEEN '" + StartDate.ToString + "' AND '" + EndDate.ToString + "'))" + _
                " ORDER BY " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "LineNumber")
                dataadapterOrderDetails = New SqlClient.SqlDataAdapter(strOrderDetailSelect, Me.ConnectionString)


                dataadapterOrderDetails.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterOrderDetails.Fill(datatableOrderDetails)

                'We need to use a custom command builder here because of the complex joins for date selection
                dcbSQLOrderDetail()

            Catch ex As Exception

            End Try

        Catch ex As Exception
            Throw New Exception("Error in Warehouse SQL Select Statement", ex) ' <GenericError> replace as needed
        End Try
        Try

            For Each curRow As DataRow In datatableWarehouses.Rows
                Dim newWarehouse As New dbiWSF.FrameworkObjects.objWarehouse(Me.WSFObjects)
                MapData(newWarehouse, curRow, datatableWarehouses.Columns, cmapWarehouses)
                newWarehouse.AttachedDataRow = curRow
                retWarehouses.Add(newWarehouse)

                Dim curDocks() As DataRow = datatableLoadingDocks.Select(Me.GetColumnMap(cmapLoadingDocks, "WarehouseID") + " = '" + newWarehouse.WarehouseID.ToString + "'")
                For Each curDock As DataRow In curDocks
                    Dim newDock As New dbiWSF.FrameworkObjects.objLoadingDock(Me.WSFObjects)
                    MapData(newDock, curDock, datatableLoadingDocks.Columns, cmapLoadingDocks)
                    newDock.AttachedDataRow = curDock
                    'Attach accepted cargo types
                    Dim datarowCargoTypes() As DataRow = datatableLoadingDockCargoKeys.Select("LoadingDockID = '" + newDock.LoadingDockID.ToString + "'")

                    For Each datarowCargoType As DataRow In datarowCargoTypes
                        newDock.CargoTypes.Add(datarowCargoType("CargoTypeID"))
                    Next

                    newWarehouse.LoadingDocks.Add(newDock)


                    Dim curShipments() As DataRow = datatableShipments.Select(Me.GetColumnMap(cmapShipments, "LoadingDockID") + " = '" + newDock.LoadingDockID.ToString + "'")

                    For Each curShipment As DataRow In curShipments
                        Dim newShipment As New dbiWSF.FrameworkObjects.objShipment(Me.WSFObjects)

                        newShipment.AttachedDataRow = curShipment
                        MapData(newShipment, curShipment, datatableShipments.Columns, cmapShipments)
                        newDock.Shipments.Add(newShipment)
                        Dim curOrders() As DataRow = datatableOrderHeaders.Select(Me.GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = '" + newShipment.ShipmentID.ToString + "'")

                        For Each curOrder As DataRow In curOrders
                            Dim newOrder As New dbiWSF.FrameworkObjects.objOrderHeader(Me.WSFObjects)
                            newOrder.AttachedDataRow = curOrder
                            MapData(newOrder, curOrder, datatableOrderHeaders.Columns, cmapOrderHeaders)
                            newShipment.OrderHeaders.Add(newOrder)

                            Dim curOrderDetails() As DataRow = datatableOrderDetails.Select(Me.GetColumnMap(cmapOrderDetails, "OrderID") + " = '" + newOrder.OrderID.ToString + "'")

                            For Each curDetail As DataRow In curOrderDetails
                                Dim newDetail As New dbiWSF.FrameworkObjects.objOrderDetail(Me.WSFObjects)
                                newDetail.AttachedDataRow = curDetail
                                MapData(newDetail, curDetail, datatableOrderDetails.Columns, cmapOrderDetails)
                                newOrder.OrderDetails.Add(newDetail)
                            Next
                        Next
                    Next
                Next
            Next
        Catch ex As Exception
        End Try
        Return retWarehouses
    End Function

#End Region

#End Region

#Region "**OLE Databinding Methods**"

#Region "--OLE Supporting Databinding--"

    ''' <summary>Supporting Method: Load the cargo types using std/inmemory OLE</summary>
    ''' <returns>A sorted list collection of the cargo type description(string) and the cargo type object</returns>
    Private Function LoadCargoOLE() As System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)
        Dim collCargoTypes As New System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)

        Try
            datatableCargoTypes.Clear()
            dataadapterCargoTypes = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objCargoType)) _
                                                        + " ORDER BY Description", Me.ConnectionString)
            Dim dcbCargoType As New OleDb.OleDbCommandBuilder(dataadapterCargoTypes)

            dataadapterCargoTypes.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterCargoTypes.Fill(datatableCargoTypes)

            For Each curRow As DataRow In datatableCargoTypes.Rows
                Dim newCargoType As New dbiWSF.FrameworkObjects.objCargoType(Me.WSFObjects)
                newCargoType.CargoID = curRow("CargoID")
                newCargoType.Description = curRow("Description")
                newCargoType.Notes = curRow("Notes")
                newCargoType.Icon = curRow("Icon")
                newCargoType.AttachedDataRow = curRow
                collCargoTypes.Add(newCargoType.Description, newCargoType)
            Next
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading CargoTypes", ex)
        End Try
        Return collCargoTypes
    End Function

    ''' <summary>Supporting Method: Load the cargo types using CustomizedOLE</summary>
    ''' <returns>A sorted list collection of the cargo type description(string) and the cargo type object</returns>
    Private Function LoadCargoCustomizedOLE() As System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)
        Dim collCargoTypes As New System.Collections.Generic.SortedList(Of String, dbiWSF.FrameworkObjects.objCargoType)

        Try
            'Load Cargo Types
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading CargoTypes", ex)
        End Try
        Return collCargoTypes
    End Function

    ''' <summary>Supporting Method: Load the loading dock exceptions using std/inmemory OLE</summary>
    ''' <returns>A collection of loading dock exceptions</returns>
    Private Function LoadLoadingDockExceptionsOLE() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Dim collExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Try
            datatableLoadingDockExceptions.Clear()
            dataadapterLoadingDockExceptions = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDockException)) _
                                                                                 , Me.ConnectionString)
            Dim dcbLoadingDockExceptions As New System.Data.OleDb.OleDbCommandBuilder(dataadapterLoadingDockExceptions)
            dataadapterLoadingDockExceptions.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterLoadingDockExceptions.Fill(datatableLoadingDockExceptions)

            datatableExceptionDockKeys.Clear()
            dataadapterExceptionDockKeys = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM ExceptionDockKeys", Me.ConnectionString)
            Dim dcbExceptionDockKeys As New System.Data.OleDb.OleDbCommandBuilder(dataadapterExceptionDockKeys)
            dataadapterExceptionDockKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterExceptionDockKeys.Fill(datatableExceptionDockKeys)


            For Each curRow As DataRow In datatableLoadingDockExceptions.Rows
                Dim newException As New dbiWSF.FrameworkObjects.objLoadingDockException(Me.WSFObjects)
                MapData(newException, curRow, datatableLoadingDockExceptions.Columns, cmapLoadingDockExceptions)
                Dim collDockIDs = From curDataRow As DataRow In datatableExceptionDockKeys.Rows _
                                   Where curDataRow("LoadingDockExceptionID") = newException.LoadingDockExceptionID _
                                   Select curDataRow("LoadingDockID")

                For Each dockID As System.Guid In collDockIDs
                    newException.LoadingDocks.Add(dockID)
                Next
                newException.AttachedDataRow = curRow
                collExceptions.Add(newException)
            Next

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Exceptions", ex)
        End Try

        Return collExceptions
    End Function

    ''' <summary>Supporting Method: Load the loading dock exceptions using CustomizedOLE</summary>
    ''' <returns>A collection of loading dock exceptions</returns>
    Private Function LoadLoadingDockExceptionsCustomizedOLE() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Dim collExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objLoadingDockException)

        Try

            'Load loading dock exceptions

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Exceptions", ex)
        End Try

        Return collExceptions

    End Function

    ''' <summary>Supporting Method: Load the recurring loading dock exceptions using std/inmemory OLE</summary>
    ''' <returns>List collection of recurrence objects</returns>
    Private Function LoadRecurringExceptionsOLE() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)

        Dim collRecurringExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)


        Try
            datatableRecurrences.Clear()
            dataadapterRecurrences = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objRecurrence)) _
                                                                         + " WHERE ObjectType = 0" _
                                                                         , Me.ConnectionString)
            Dim dcbRecurrences As New System.Data.OleDb.OleDbCommandBuilder(dataadapterRecurrences)
            dataadapterRecurrences.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterRecurrences.Fill(datatableRecurrences)

            datatableRecurrenceObjectKeys.Clear()
            dataadapterRecurrenceObjectKeys = New System.Data.OleDb.OleDbDataAdapter("SELECT * FROM RecurrenceObjectKeys", Me.ConnectionString)
            Dim dcbRecurrenceObjectKeys As New System.Data.OleDb.OleDbCommandBuilder(dataadapterRecurrenceObjectKeys)
            dataadapterRecurrenceObjectKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterRecurrenceObjectKeys.Fill(datatableRecurrenceObjectKeys)


            For Each curRow As DataRow In datatableRecurrences.Rows
                Dim newRecurrence As New dbiWSF.FrameworkObjects.objRecurrence(Me.WSFObjects)
                MapData(newRecurrence, curRow, datatableRecurrences.Columns, cmapRecurrences)
                Dim collDockIDs = From curDataRow As DataRow In datatableRecurrenceObjectKeys.Rows _
                                   Where curDataRow("RecurrenceID") = newRecurrence.RecurrenceID _
                                   Select curDataRow("ObjectID")

                For Each dockID As System.Guid In collDockIDs
                    newRecurrence.RecurringObjects.Add(dockID)
                Next

                newRecurrence.AttachedDataRow = curRow
                collRecurringExceptions.Add(newRecurrence)
            Next

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Recurrence", ex)
        End Try
        Return collRecurringExceptions
    End Function

    ''' <summary>Supporting Method: Load the recurring loading dock exceptions using CustomizedOLE</summary>
    ''' <returns>List collection of recurrence objects</returns>
    Private Function LoadRecurringExceptionsCustomizedOLE() As System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)
        Dim collRecurringExceptions As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objRecurrence)

        Try
            'Load recurring exceptions

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error loading Recurrence", ex)
        End Try
        Return collRecurringExceptions
    End Function

    ''' <summary>
    ''' Supporting Method: Loads unassigned order headers into the OrderHeader datatable
    ''' </summary>
    ''' <remarks>Because the OrderHeaders are loaded by Shipment, those Order Headers that don't
    ''' belong to a shipment aren't loaded when the main dataloads occur. This method will compensate for that.</remarks>
    Private Sub LoadUnassignedOrderHeadersOLE()
        Dim daLoadUnassignedOH As OleDb.OleDbDataAdapter
        Dim daLoadUnassignedOD As OleDb.OleDbDataAdapter

        Dim strOrderHeaderTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader))
        Dim strOrderDetailTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail))

        Dim strOrderHeaderSelect As String = "SELECT " + strOrderHeaderTableName + ".* FROM " + strOrderHeaderTableName + _
        " WHERE " + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = '{" + System.Guid.Empty.ToString + "}'" + _
        " ORDER BY " + GetColumnMap(cmapOrderHeaders, "OrderEntryID")

        daLoadUnassignedOH = New OleDb.OleDbDataAdapter(strOrderHeaderSelect, Me.ConnectionString)
        daLoadUnassignedOH.MissingSchemaAction = MissingSchemaAction.AddWithKey

        daLoadUnassignedOH.Fill(datatableOrderHeaders)

        Dim strOrderDetailSelect As String = "SELECT " + strOrderDetailTableName + ".* FROM (" + strOrderDetailTableName + _
                 " INNER JOIN " + strOrderHeaderTableName + _
                 " ON " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "OrderID") + " = " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "OrderID") + ")" + _
                 " WHERE " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = '{" + System.Guid.Empty.ToString + "}'" + _
                 " ORDER BY " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "LineNumber")

        daLoadUnassignedOD = New OleDb.OleDbDataAdapter(strOrderDetailSelect, Me.ConnectionString)
        daLoadUnassignedOD.MissingSchemaAction = MissingSchemaAction.AddWithKey

        daLoadUnassignedOD.Fill(datatableOrderDetails)
    End Sub

    ''' <summary>Supporting Method: Add the insert, update and delete commands
    ''' to the OLEdataadapter for Order Headers</summary>
    Private Sub dcbOLEDBOrderHeader()
        Dim orderIDMap As String = GetColumnMap(cmapOrderHeaders, "OrderID")
        Try

            Dim datacommandUpdate As New OleDb.OleDbCommand()

            Dim OLEDBOHUpdate As String
            OLEDBOHUpdate = "UPDATE " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) + " SET "

            For Each DataColumnToAdd As DataColumn In datatableOrderHeaders.Columns
                OLEDBOHUpdate += DataColumnToAdd.ColumnName + " = ?, "
                Dim newParameter As New OleDb.OleDbParameter()
                newParameter.DbType = GetOLEDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.Input
                newParameter.ParameterName = DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                newParameter.SourceVersion = DataRowVersion.Current
                datacommandUpdate.Parameters.Add(newParameter)
            Next


            For Each DataColumnToAdd As DataColumn In datatableOrderHeaders.Columns
                'OLEDBOHUpdate += DataColumnToAdd.ColumnName + " = ?, "
                Dim newParameter As New OleDb.OleDbParameter()
                newParameter.DbType = GetOLEDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.Input
                newParameter.ParameterName = "original_" + DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                newParameter.SourceVersion = DataRowVersion.Original
                datacommandUpdate.Parameters.Add(newParameter)
            Next


            OLEDBOHUpdate = OLEDBOHUpdate.Remove(OLEDBOHUpdate.Length - 2)

            OLEDBOHUpdate += " WHERE (" + orderIDMap + " = ?)"
            datacommandUpdate.CommandText = OLEDBOHUpdate
            datacommandUpdate.Connection = New OleDb.OleDbConnection(Me.ConnectionString)
            Me.dataadapterOrderHeaders.UpdateCommand = datacommandUpdate

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Update Command to dataadapterOrderHeader", ex)
        End Try


        Try
            Dim datacommandInsert As New OleDb.OleDbCommand

            Dim OLEDBOHInsert As String
            Dim OLEDBValueString As String = ""
            OLEDBOHInsert = "INSERT INTO " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) + " ("
            For Each DataColumnToAdd As DataColumn In datatableOrderHeaders.Columns
                OLEDBOHInsert += DataColumnToAdd.ColumnName + ", "
                OLEDBValueString += "?, "

                Dim newParameter As New OleDb.OleDbParameter()
                newParameter.DbType = GetOLEDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.Input
                newParameter.ParameterName = DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                datacommandInsert.Parameters.Add(newParameter)
            Next

            OLEDBOHInsert = OLEDBOHInsert.Remove(OLEDBOHInsert.Length - 2)
            OLEDBValueString = OLEDBValueString.Remove(OLEDBValueString.Length - 2)
            OLEDBOHInsert += ") VALUES(" + OLEDBValueString + ")"
            datacommandInsert.CommandText = OLEDBOHInsert
            datacommandInsert.Connection = New OleDb.OleDbConnection(Me.ConnectionString)
            Me.dataadapterOrderHeaders.InsertCommand = datacommandInsert

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Insert Command to dataadapterOrderHeader", ex)
        End Try

        Try
            Dim datacommandDelete As New OleDb.OleDbCommand
            Dim newParameter As New OleDb.OleDbParameter("@" + orderIDMap, OleDb.OleDbType.Guid, 0, orderIDMap)
            datacommandDelete.Parameters.Add(newParameter)
            datacommandDelete.CommandText = "DELETE FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) + " WHERE (" + orderIDMap + " = @" + orderIDMap + ")"
            datacommandDelete.Connection = New OleDb.OleDbConnection(Me.ConnectionString)
            Me.dataadapterOrderHeaders.DeleteCommand = datacommandDelete
        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Delete Command to dataadapterOrderHeader", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Add the insert, update and delete commands
    ''' to the OLEdataadapter for Order Details</summary>
    Private Sub dcbOLEDBOrderDetail()
        Dim orderIDMap As String = GetColumnMap(cmapOrderDetails, "OrderDetailID")
        Try

            Dim datacommandUpdate As New OleDb.OleDbCommand()

            Dim OLEDBOHUpdate As String
            OLEDBOHUpdate = "UPDATE " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) + " SET "

            For Each DataColumnToAdd As DataColumn In datatableOrderDetails.Columns
                OLEDBOHUpdate += DataColumnToAdd.ColumnName + " = ?, "
                Dim newParameter As New OleDb.OleDbParameter()
                newParameter.DbType = GetOLEDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.Input
                newParameter.ParameterName = DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                newParameter.SourceVersion = DataRowVersion.Current
                datacommandUpdate.Parameters.Add(newParameter)
            Next


            For Each DataColumnToAdd As DataColumn In datatableOrderDetails.Columns
                Dim newParameter As New OleDb.OleDbParameter()
                newParameter.DbType = GetOLEDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.Input
                newParameter.ParameterName = "original_" + DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                newParameter.SourceVersion = DataRowVersion.Original
                datacommandUpdate.Parameters.Add(newParameter)
            Next


            OLEDBOHUpdate = OLEDBOHUpdate.Remove(OLEDBOHUpdate.Length - 2)

            OLEDBOHUpdate += " WHERE (" + orderIDMap + " = ?)"
            datacommandUpdate.CommandText = OLEDBOHUpdate
            datacommandUpdate.Connection = New OleDb.OleDbConnection(Me.ConnectionString)
            Me.dataadapterOrderDetails.UpdateCommand = datacommandUpdate

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Update Command to dataadapterOrderdetail", ex)
        End Try


        Try
            Dim datacommandInsert As New OleDb.OleDbCommand

            Dim OLEDBOHInsert As String
            Dim OLEDBValueString As String = ""
            OLEDBOHInsert = "INSERT INTO " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) + " ("
            For Each DataColumnToAdd As DataColumn In datatableOrderDetails.Columns
                OLEDBOHInsert += DataColumnToAdd.ColumnName + ", "
                OLEDBValueString += "?, "

                Dim newParameter As New OleDb.OleDbParameter()
                newParameter.DbType = GetOLEDBType(DataColumnToAdd.DataType)
                newParameter.Direction = ParameterDirection.Input
                newParameter.ParameterName = DataColumnToAdd.ColumnName
                newParameter.SourceColumn = DataColumnToAdd.ColumnName
                newParameter.Size = DataColumnToAdd.MaxLength
                datacommandInsert.Parameters.Add(newParameter)
            Next

            OLEDBOHInsert = OLEDBOHInsert.Remove(OLEDBOHInsert.Length - 2)
            OLEDBValueString = OLEDBValueString.Remove(OLEDBValueString.Length - 2)
            OLEDBOHInsert += ") VALUES(" + OLEDBValueString + ")"
            datacommandInsert.CommandText = OLEDBOHInsert
            datacommandInsert.Connection = New OleDb.OleDbConnection(Me.ConnectionString)
            Me.dataadapterOrderDetails.InsertCommand = datacommandInsert

        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Insert Command to dataadapterOrderDetail", ex)
        End Try

        Try
            Dim datacommandDelete As New OleDb.OleDbCommand
            Dim newParameter As New OleDb.OleDbParameter("@" + orderIDMap, OleDb.OleDbType.Guid, 0, orderIDMap)
            datacommandDelete.Parameters.Add(newParameter)
            datacommandDelete.CommandText = "DELETE FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) + " WHERE (" + orderIDMap + " = @" + orderIDMap + ")"
            datacommandDelete.Connection = New OleDb.OleDbConnection(Me.ConnectionString)
            Me.dataadapterOrderDetails.DeleteCommand = datacommandDelete
        Catch ex As Exception
            '<Generic Error> - replace as needed
            Throw New Exception("Error adding Delete Command to dataadapterOrderDetail", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Gets the OLEDBType from a base type</summary>
    ''' <param name="TypeToConvert">The base system type to convert to DBType</param>
    Private Function GetOLEDBType(ByVal TypeToConvert As System.Type) As DbType
        Try

            Dim p1 As OleDb.OleDbParameter
            Dim tc As System.ComponentModel.TypeConverter
            p1 = New OleDb.OleDbParameter()
            tc = System.ComponentModel.TypeDescriptor.GetConverter(p1.DbType)
            If tc.CanConvertFrom(TypeToConvert) Then
                p1.DbType = tc.ConvertFrom(TypeToConvert.Name)
            Else
                Try
                    p1.DbType = tc.ConvertFrom(TypeToConvert.Name)
                Catch ex As Exception
                    p1.DbType = DbType.String
                End Try
            End If
            Return p1.DbType

        Catch ex As Exception
            Return DbType.String
        End Try

    End Function

#End Region

#Region "--Standard Databinding--"

    ''' <summary>Supporting Method: Load the Framework data using OLE and
    ''' <see cref="dbiWSF.Databinding.DataBindingMethods"> Standard data binding</see></summary>
    ''' <remarks>Standard data binding loads the tables in the order they are needed to generate the objects. This
    ''' classed loading can take some time as there is no database optimization added. We've gone with a flat
    ''' table structure to ease the update process, however this can be quite slow. We recommend using InMemory data binding or 
    ''' if you need even more flexibility use the Customized data model.</remarks>
    ''' <returns>A list collection of warehouse objects</returns>
    Private Function LoadStandardOLE() As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Try
            datatableWarehouses.Clear()
            dataadapterWarehouses = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objWarehouse)) _
                                                        + " ORDER BY Description", Me.ConnectionString)
            Dim dcbWarehouses As New OleDb.OleDbCommandBuilder(dataadapterWarehouses)

            dataadapterWarehouses.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterWarehouses.Fill(datatableWarehouses)


            For Each curRow As DataRow In datatableWarehouses.Rows
                Dim newWarehouse As New dbiWSF.FrameworkObjects.objWarehouse(Me.WSFObjects)
                MapData(newWarehouse, curRow, datatableWarehouses.Columns, cmapWarehouses)
                newWarehouse.AttachedDataRow = curRow
                LoadLoadingDocksOLE(newWarehouse)
                retWarehouses.Add(newWarehouse)
            Next
            Try
                LoadStandardUnassignedShipmentsOLE()
            Catch ex As Exception
                Throw New Exception("Error in LoadUnassignedShipments", ex) ' <GenericError> replace as needed
            End Try
        Catch ex As Exception
            Throw New Exception("Error in Warehouse Select Statement", ex) ' <GenericError> replace as needed
        End Try
        Return retWarehouses
    End Function

    ''' <summary>Supporting Method: Used for OLE STD</summary>
    ''' <param name="Warehouse">The warehouse object to load the loading docks for</param>
    Private Sub LoadLoadingDocksOLE(ByRef Warehouse As dbiWSF.FrameworkObjects.objWarehouse)
        Dim curWarehouseID As System.Guid = Warehouse.WarehouseID

        Try
            dataadapterLoadingDocks = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDock)) _
                                                   + " WHERE " + GetColumnMap(cmapLoadingDocks, "WarehouseID") + " like '{" + Warehouse.WarehouseID.ToString _
                                                   + "}' ORDER BY WarehouseOrdinal", Me.ConnectionString)
            Dim dcbDocks As New OleDb.OleDbCommandBuilder(dataadapterLoadingDocks)

            dataadapterLoadingDocks.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterLoadingDocks.Fill(datatableLoadingDocks)

            Dim colDocks = From selectItems As DataRow In datatableLoadingDocks.Rows _
                           Where selectItems.Item(GetColumnMap(cmapLoadingDocks, "WarehouseID")) = curWarehouseID _
                           Select selectItems


            For Each curRow As DataRow In colDocks
                Dim newDock As New dbiWSF.FrameworkObjects.objLoadingDock(Me.WSFObjects)
                MapData(newDock, curRow, datatableLoadingDocks.Columns, cmapLoadingDocks)
                newDock.AttachedDataRow = curRow


                Try
                    dataadapterLoadingDockCargoKeys = New OleDb.OleDbDataAdapter("SELECT * FROM LoadingDockCargoKeys", Me.ConnectionString)
                    Dim dcbLoadingDockCargoKeys As New OleDb.OleDbCommandBuilder(dataadapterLoadingDockCargoKeys)

                    dataadapterLoadingDockCargoKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
                    dataadapterLoadingDockCargoKeys.Fill(datatableLoadingDockCargoKeys)

                    'Attach accepted cargo types
                    Dim datarowCargoTypes() As DataRow = datatableLoadingDockCargoKeys.Select("LoadingDockID = '" + newDock.LoadingDockID.ToString + "'")

                    For Each datarowCargoType As DataRow In datarowCargoTypes
                        newDock.CargoTypes.Add(datarowCargoType("CargoTypeID"))
                    Next

                Catch ex As Exception
                    '<GenericError> Replace as needed.
                    Throw New Exception("Error in Loading Dock Cargo Types Load", ex)
                End Try

                LoadStandardShipmentsOLE(newDock)
                Warehouse.LoadingDocks.Add(newDock)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Dock Load", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for OLE STD</summary>
    Private Sub LoadStandardShipmentsOLE(ByRef LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock)
        Dim curLoadingDockID As System.Guid = LoadingDock.LoadingDockID

        Try
            dataadapterShipments = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment)) _
                                                       + " WHERE " + GetColumnMap(cmapShipments, "LoadingDockID") _
                                                       + " like '{" + LoadingDock.LoadingDockID.ToString + "}' ORDER BY SchedStartTime", Me.ConnectionString)
            Dim dcbShipments As New OleDb.OleDbCommandBuilder(dataadapterShipments)

            dataadapterShipments.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterShipments.Fill(datatableShipments)

            Dim colShipments = From selectItems As DataRow In datatableShipments.Rows _
               Where selectItems.Item(GetColumnMap(cmapShipments, "LoadingDockID")) = curLoadingDockID _
               Select selectItems


            For Each curRow As DataRow In colShipments
                Dim newShipment As New dbiWSF.FrameworkObjects.objShipment(Me.WSFObjects)
                newShipment.AttachedDataRow = curRow
                MapData(newShipment, curRow, datatableShipments.Columns, cmapShipments)
                LoadStandardOrderHeaderOLE(newShipment)
                LoadingDock.Shipments.Add(newShipment)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Shipments", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for OLE STD</summary>
    Private Sub LoadStandardOrderHeaderOLE(ByRef Shipment As dbiWSF.FrameworkObjects.objShipment)
        Dim curShipmentID As System.Guid = Shipment.ShipmentID

        Try
            dataadapterOrderHeaders = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader)) _
                                                         + " WHERE " + GetColumnMap(cmapOrderHeaders, "ShipmentID") _
                                                         + " like '{" + Shipment.ShipmentID.ToString + "}' ORDER BY OrderEntryID", Me.ConnectionString)
            Dim dcbOrderHeaders As New OleDb.OleDbCommandBuilder(dataadapterOrderHeaders)
            Beep()

            dataadapterOrderHeaders.MissingSchemaAction = MissingSchemaAction.AddWithKey

            dataadapterOrderHeaders.Fill(datatableOrderHeaders)

            Dim colOrderHeaders = From selectItems As DataRow In datatableOrderHeaders.Rows _
              Where selectItems.Item(GetColumnMap(cmapOrderHeaders, "ShipmentID")) = curShipmentID _
              Select selectItems

            For Each curRow As DataRow In colOrderHeaders
                Dim newOrderHeader As New dbiWSF.FrameworkObjects.objOrderHeader(Me.WSFObjects)
                newOrderHeader.AttachedDataRow = curRow
                MapData(newOrderHeader, curRow, datatableOrderHeaders.Columns, cmapOrderHeaders)
                LoadStandardOrderDetailOLE(newOrderHeader)
                Shipment.OrderHeaders.Add(newOrderHeader)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Order Headers Standard OLE", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for OLE STD</summary>
    Private Sub LoadStandardOrderDetailOLE(ByRef OrderHeader As dbiWSF.FrameworkObjects.objOrderHeader)
        Dim curOrderHeaderID As System.Guid = OrderHeader.OrderID

        Try
            dataadapterOrderDetails = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail)) _
                                                         + " WHERE " + GetColumnMap(cmapOrderDetails, "OrderID") _
                                                         + " like '{" + OrderHeader.OrderID.ToString + "}' ORDER BY LineNumber", Me.ConnectionString)
            Dim dcbOrderDetails As New OleDb.OleDbCommandBuilder(dataadapterOrderDetails)
            dataadapterOrderDetails.MissingSchemaAction = MissingSchemaAction.AddWithKey

            dataadapterOrderDetails.Fill(datatableOrderDetails)

            Dim colOrderDetails = From selectItems As DataRow In datatableOrderDetails.Rows _
              Where selectItems.Item(GetColumnMap(cmapOrderDetails, "OrderID")) = curOrderHeaderID _
              Select selectItems

            For Each curRow As DataRow In colOrderDetails
                Dim newOrderDetail As New dbiWSF.FrameworkObjects.objOrderDetail(Me.WSFObjects)
                newOrderDetail.AttachedDataRow = curRow
                MapData(newOrderDetail, curRow, datatableOrderDetails.Columns, cmapOrderDetails)
                OrderHeader.OrderDetails.Add(newOrderDetail)
            Next

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Order Details Standard OLE", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for OLE STD</summary>
    Private Sub LoadStandardUnassignedShipmentsOLE()
        Try
            dataadapterShipments = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment)) _
                                                       + " WHERE " + GetColumnMap(cmapShipments, "LoadingDockID") _
                                                       + " IS NULL ORDER BY SchedStartTime", Me.ConnectionString)
            Dim dcbShipments As New OleDb.OleDbCommandBuilder(dataadapterShipments)

            dataadapterShipments.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterShipments.Fill(datatableShipments)

        Catch ex As Exception
            '<GenericError> Replace as needed.
            Throw New Exception("Error in Loading Unassigned ", ex)
        End Try
    End Sub

#End Region

#Region "--Customized Databinding--"
    ''' <summary>Supporting Method: Load the Framework data using OLE and
    ''' <see cref="dbiWSF.Databinding.DataBindingMethods"> Customized data binding</see>
    ''' </summary>
    ''' <remarks>Note: by default there is no code in this function, this is a placeholder for custom OLE
    ''' Statements to connect to an OLE database. It is up to the developer to create the CRUD for said code and
    ''' to ensure it flows through the rest of the data binding class. This is where you would sync the data tables
    ''' to stored procedures or custom views/queries to the databases.</remarks>
    Private Function LoadCustomizedOLE() As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        'Ensure that you populate the data tables as necessary
        'Also be sure to modify the Update, Insert and Delete commands on the
        'appropriate data adapters
        dataadapterWarehouses.UpdateCommand.CommandText = ""
        dataadapterWarehouses.InsertCommand.CommandText = ""
        dataadapterWarehouses.DeleteCommand.CommandText = ""

        dataadapterLoadingDocks.UpdateCommand.CommandText = ""
        dataadapterLoadingDocks.InsertCommand.CommandText = ""
        dataadapterLoadingDocks.DeleteCommand.CommandText = ""

        dataadapterShipments.UpdateCommand.CommandText = ""
        dataadapterShipments.InsertCommand.CommandText = ""
        dataadapterShipments.DeleteCommand.CommandText = ""

        Return retWarehouses
    End Function

#End Region

#Region "--InMemory Databinding--"
    ''' <summary>Supporting Method: Load the Framework data using OLE and
    ''' <see cref="dbiWSF.Databinding.DataBindingMethods"> InMemory data binding</see>
    ''' </summary>
    ''' <remarks>InMemory data binding loads all the data requested into memory first then uses
    ''' LINQ to parse out the data to the objects. This avoids multiple JOIN statements and custom
    ''' coding which greatly improves performance. If you need more control use the Customized data binding model.</remarks>
    ''' <returns>A list collection of warehouse objects</returns>
    ''' <param name="StartDate">The start date/time for the data being loaded.</param>
    ''' <param name="EndDate">The end date/time for the data being loaded.</param>
    Private Function LoadInMemoryOLE(ByVal StartDate As Date, ByVal EndDate As Date) As Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)
        Dim retWarehouses As New Collections.Generic.List(Of dbiWSF.FrameworkObjects.objWarehouse)

        Try
            datatableWarehouses.Clear()
            dataadapterWarehouses = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objWarehouse)) _
                                                        + " ORDER BY Description", Me.ConnectionString)
            Dim dcbWarehouses As New OleDb.OleDbCommandBuilder(dataadapterWarehouses)

            dataadapterWarehouses.MissingSchemaAction = MissingSchemaAction.AddWithKey
            dataadapterWarehouses.Fill(datatableWarehouses)

            datatableLoadingDocks.Clear()

            Try
                dataadapterLoadingDocks = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDock)) _
                                                       + " ORDER BY WarehouseOrdinal", Me.ConnectionString)
                Dim dcbDocks As New OleDb.OleDbCommandBuilder(dataadapterLoadingDocks)

                dataadapterLoadingDocks.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterLoadingDocks.Fill(datatableLoadingDocks)

            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Dock Load", ex)
            End Try

            Try
                dataadapterLoadingDockExceptions = New OleDb.OleDbDataAdapter("SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objLoadingDockException)) _
                                                       + " ORDER BY StartDate", Me.ConnectionString)

                Dim dcbLoadingDockExceptions As New OleDb.OleDbCommandBuilder(dataadapterLoadingDockExceptions)

                dataadapterLoadingDockExceptions.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterLoadingDockExceptions.Fill(datatableLoadingDockExceptions)
            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Dock Exception Load", ex)
            End Try

            Try
                dataadapterLoadingDockCargoKeys = New OleDb.OleDbDataAdapter("SELECT * FROM LoadingDockCargoKeys", Me.ConnectionString)
                Dim dcbLoadingDockCargoKeys As New OleDb.OleDbCommandBuilder(dataadapterLoadingDockCargoKeys)

                dataadapterLoadingDockCargoKeys.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterLoadingDockCargoKeys.Fill(datatableLoadingDockCargoKeys)
            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Dock Cargo Types Load", ex)
            End Try


            Try

                Dim strShipmentSelect As String = "SELECT * FROM " + GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment)) + _
                " WHERE ((" + GetColumnMap(cmapShipments, "SchedStartTime") + " BETWEEN #" & StartDate.ToString & "# AND #" & EndDate.ToString & "#)" + _
                " OR (" + GetColumnMap(cmapShipments, "SchedEndTime") + " BETWEEN #" & StartDate.ToString & "# AND #" & EndDate.ToString & "#))" + _
                " ORDER BY " + GetColumnMap(cmapShipments, "SchedStartTime")

                dataadapterShipments = New OleDb.OleDbDataAdapter(strShipmentSelect, Me.ConnectionString)
                Dim dcbShipments As New OleDb.OleDbCommandBuilder(dataadapterShipments)

                dataadapterShipments.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterShipments.Fill(datatableShipments)

            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Shipments", ex)
            End Try

            Try
                Dim strOrderHeaderTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader))
                Dim strShipmentTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment))

                Dim strOrderHeaderSelect As String = "SELECT " + strOrderHeaderTableName + ".* FROM (" + strOrderHeaderTableName + _
                " INNER JOIN " + strShipmentTableName + _
                " ON " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = " + strShipmentTableName + "." + GetColumnMap(cmapShipments, "ShipmentID") + ")" + _
                " WHERE ((" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedStartTime") + _
                " BETWEEN #" + StartDate.ToString + "# AND #" + EndDate.ToString + "#)" + _
                " OR (" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedEndTime") + _
                " BETWEEN #" + StartDate.ToString + "# AND #" + EndDate.ToString + "#))" + _
                " ORDER BY " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID")


                dataadapterOrderHeaders = New OleDb.OleDbDataAdapter(strOrderHeaderSelect, Me.ConnectionString)


                dataadapterOrderHeaders.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterOrderHeaders.Fill(datatableOrderHeaders)

                dcbOLEDBOrderHeader()

            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Order Headers", ex)
            End Try


            Try
                Dim strOrderHeaderTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderHeader))
                Dim strOrderDetailTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objOrderDetail))
                Dim strShipmentTableName As String = GetTableName(GetType(dbiWSF.FrameworkObjects.objShipment))

                Dim strOrderDetailSelect As String = "SELECT " + strOrderDetailTableName + ".* FROM (" + strOrderDetailTableName + _
                " INNER JOIN " + strOrderHeaderTableName + _
                " ON " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "OrderID") + " = " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "OrderID") + ")" + _
                " INNER JOIN " + strShipmentTableName + _
                " ON " + strOrderHeaderTableName + "." + GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = " + strShipmentTableName + "." + GetColumnMap(cmapShipments, "ShipmentID") + _
                " WHERE ((" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedStartTime") + _
                " BETWEEN #" + StartDate.ToString + "# AND #" + EndDate.ToString + "#) " + _
                " OR (" + strShipmentTableName + "." + GetColumnMap(cmapShipments, "SchedEndTime") + _
                " BETWEEN #" + StartDate.ToString + "# AND #" + EndDate.ToString + "#))" + _
                " ORDER BY " + strOrderDetailTableName + "." + GetColumnMap(cmapOrderDetails, "LineNumber")

                dataadapterOrderDetails = New OleDb.OleDbDataAdapter(strOrderDetailSelect, Me.ConnectionString)

                dataadapterOrderDetails.MissingSchemaAction = MissingSchemaAction.AddWithKey
                dataadapterOrderDetails.Fill(datatableOrderDetails)

                dcbOLEDBOrderDetail()


            Catch ex As Exception
                '<GenericError> Replace as needed.
                Throw New Exception("Error in Loading Order Details", ex)
            End Try

        Catch ex As Exception
            ' <GenericError> replace as needed
            Throw New Exception("Error in Warehouse OLE Select Statement", ex)
        End Try
        Try

            For Each curRow As DataRow In datatableWarehouses.Rows
                Dim newWarehouse As New dbiWSF.FrameworkObjects.objWarehouse(Me.WSFObjects)
                MapData(newWarehouse, curRow, datatableWarehouses.Columns, cmapWarehouses)
                newWarehouse.AttachedDataRow = curRow
                retWarehouses.Add(newWarehouse)

                Dim curDocks() As DataRow = datatableLoadingDocks.Select(Me.GetColumnMap(cmapLoadingDocks, "WarehouseID") + " = '" + newWarehouse.WarehouseID.ToString + "'")
                For Each curDock As DataRow In curDocks
                    Dim newDock As New dbiWSF.FrameworkObjects.objLoadingDock(Me.WSFObjects)
                    MapData(newDock, curDock, datatableLoadingDocks.Columns, cmapLoadingDocks)
                    newDock.AttachedDataRow = curDock
                    'Attach accepted cargo types
                    Dim datarowCargoTypes() As DataRow = datatableLoadingDockCargoKeys.Select("LoadingDockID = '" + newDock.LoadingDockID.ToString + "'")

                    For Each datarowCargoType As DataRow In datarowCargoTypes
                        newDock.CargoTypes.Add(datarowCargoType("CargoTypeID"))
                    Next

                    newWarehouse.LoadingDocks.Add(newDock)

                    Dim curShipments() As DataRow = datatableShipments.Select(Me.GetColumnMap(cmapShipments, "LoadingDockID") + " = '" + newDock.LoadingDockID.ToString + "'")

                    For Each curShipment As DataRow In curShipments
                        Dim newShipment As New dbiWSF.FrameworkObjects.objShipment(Me.WSFObjects)
                        newShipment.AttachedDataRow = curShipment
                        MapData(newShipment, curShipment, datatableShipments.Columns, cmapShipments)
                        newDock.Shipments.Add(newShipment)

                        Dim curOrders() As DataRow = datatableOrderHeaders.Select(Me.GetColumnMap(cmapOrderHeaders, "ShipmentID") + " = '" + newShipment.ShipmentID.ToString + "'")

                        For Each curOrder As DataRow In curOrders
                            Dim newOrder As New dbiWSF.FrameworkObjects.objOrderHeader(Me.WSFObjects)
                            newOrder.AttachedDataRow = curOrder
                            MapData(newOrder, curOrder, datatableOrderHeaders.Columns, cmapOrderHeaders)
                            newShipment.OrderHeaders.Add(newOrder)

                            Dim curOrderDetails() As DataRow = datatableOrderDetails.Select(Me.GetColumnMap(cmapOrderDetails, "OrderID") + " = '" + newOrder.OrderID.ToString + "'")

                            For Each curDetail As DataRow In curOrderDetails
                                Dim newDetail As New dbiWSF.FrameworkObjects.objOrderDetail(Me.WSFObjects)
                                newDetail.AttachedDataRow = curDetail
                                MapData(newDetail, curDetail, datatableOrderDetails.Columns, cmapOrderDetails)
                                newOrder.OrderDetails.Add(newDetail)
                            Next
                        Next
                    Next
                Next
            Next
        Catch ex As Exception
            '<Generic Error>
            Throw New Exception("Error in Processing Data from adapters", ex)
        End Try
        Return retWarehouses
    End Function


#End Region

#End Region

#Region "**Database Updates**"

    ''' <summary>Update all the data adapters with their respective data tables</summary>
    Public Sub ManualUpdateDatabases()
        Try
            dataadapterRecurrences.Update(datatableRecurrences)
            dataadapterLoadingDockExceptions.Update(datatableLoadingDockExceptions)
            dataadapterWarehouses.Update(datatableWarehouses)
            dataadapterLoadingDocks.Update(datatableLoadingDocks)
            dataadapterShipments.Update(datatableShipments)
            dataadapterOrderHeaders.Update(datatableOrderHeaders)
            dataadapterOrderDetails.Update(datatableOrderDetails)
            dataadapterExceptionDockKeys.Update(datatableExceptionDockKeys)
            dataadapterLoadingDockCargoKeys.Update(datatableLoadingDockCargoKeys)
            dataadapterRecurrenceObjectKeys.Update(datatableRecurrenceObjectKeys)
        Catch exDB As DBConcurrencyException
            '<GenericError> Concurrency Error, trap and handle as appropriate
            Throw New Exception("Manual Table Update Data concurrency exception", exDB)
        Catch ex As Exception
            '<GenericError> Update exception, trap and handle as appropriate
            Throw New Exception("Manual Data update error...", ex)
        End Try

    End Sub

    ''' <summary>Cancels all changes to the databases since the last update.</summary>
    Public Sub ManualCancelDatabaseChanges()
        Try
            datatableRecurrences.RejectChanges()
            datatableLoadingDockExceptions.RejectChanges()
            datatableWarehouses.RejectChanges()
            datatableLoadingDocks.RejectChanges()
            datatableShipments.RejectChanges()
            datatableOrderHeaders.RejectChanges()
            datatableOrderDetails.RejectChanges()
            datatableExceptionDockKeys.RejectChanges()
            datatableLoadingDockCargoKeys.RejectChanges()
            datatableRecurrenceObjectKeys.RejectChanges()

        Catch exDB As DBConcurrencyException
            '<GenericError> Concurrency Error, trap and handle as appropriate
            Throw New Exception("Manual Table RejectChanges concurrency exception", exDB)
        Catch ex As Exception
            '<GenericError> Update exception, trap and handle as appropriate
            Throw New Exception("Manual Data RejectChanges error...", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Internal call for updating the data adapters, wrapped in an auto update as
    ''' individual objects can call this method when properties are set</summary>
    Private Sub UpdateDatabases()
        If AutoUpdateData = True Then
            Try
                dataadapterRecurrences.Update(datatableRecurrences)
                dataadapterLoadingDockExceptions.Update(datatableLoadingDockExceptions)
                dataadapterWarehouses.Update(datatableWarehouses)
                dataadapterLoadingDocks.Update(datatableLoadingDocks)
                dataadapterShipments.Update(datatableShipments)
                dataadapterOrderHeaders.Update(datatableOrderHeaders)
                dataadapterOrderDetails.Update(datatableOrderDetails)
                dataadapterExceptionDockKeys.Update(datatableExceptionDockKeys)
                dataadapterLoadingDockCargoKeys.Update(datatableLoadingDockCargoKeys)
                dataadapterRecurrenceObjectKeys.Update(datatableRecurrenceObjectKeys)
            Catch exDB As DBConcurrencyException
                Throw New Exception("Update All Data concurrency exception", exDB)
            Catch ex As Exception
                Throw New Exception("Update All Data update error...", ex)
            End Try
        End If

    End Sub

    ''' <summary>Supporting Method: Updates an individual data adapter and data table pair</summary>
    ''' <param name="dataadapterToUpdate">The dataadapter to update</param>
    ''' <param name="datatableToUpdate">The datatable to update</param>
    Private Sub UpdateIndividualDatatable(ByRef dataadapterToUpdate As Object, ByRef datatableToUpdate As DataTable)
        Try
            dataadapterToUpdate.Update(datatableToUpdate)
        Catch exDB As DBConcurrencyException
            Throw New Exception("Individual Data concurrency exception, TABLE: " & datatableToUpdate.TableName, exDB)
        Catch ex As Exception
            Throw New Exception("Individual Data update error... TABLE: " & datatableToUpdate.TableName, ex)
        End Try

    End Sub


#Region "--Warehouses--"

    ''' <summary>Adds a new warehouse to the database</summary>
    ''' <param name="NewWarehouseObject">The warehouse object used to create a new record in the database</param>
    Public Sub AddNewWarehouse(ByRef NewWarehouseObject As dbiWSF.FrameworkObjects.objWarehouse)
        Dim newDataRow As DataRow = Me.datatableWarehouses.NewRow

        Me.ObjectPropertyToDatarow(NewWarehouseObject, newDataRow, Me.datatableWarehouses.Columns, cmapWarehouses)

        Me.datatableWarehouses.Rows.Add(newDataRow)
        NewWarehouseObject.AttachedDataRow = newDataRow
        UpdateWarehouses()

    End Sub

    ''' <summary>Removes a warehouse from the database</summary>
    ''' <param name="WarehouseObjectToDelete">The warehouse object to delete from the database</param>
    Public Sub RemoveWarehouse(ByRef WarehouseObjectToDelete As dbiWSF.FrameworkObjects.objWarehouse)
        WarehouseObjectToDelete.AttachedDataRow.Delete()
        UpdateWarehouses()
    End Sub

    ''' <summary>A manual method for updating the warehouse database</summary>
    Public Sub ManualWarehouseUpdate()
        Try
            dataadapterWarehouses.Update(datatableWarehouses)
        Catch ex As Exception
            Throw New Exception("Error in ManualWarehouseUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the warehouse AttachedDataRow with the properties on the warehouse</summary>
    ''' <param name="warehouseToUpdate">The warehouse object to update</param>
    Public Sub UpdateWarehouseDataRow(ByRef warehouseToUpdate As dbiWSF.FrameworkObjects.objWarehouse)
        If Not warehouseToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(warehouseToUpdate, warehouseToUpdate.AttachedDataRow, Me.datatableWarehouses.Columns, cmapWarehouses)
            UpdateWarehouses()
        End If
    End Sub

    ''' <summary>Supporting Method: Used for updating the warehouse data table when properties are set</summary>
    Private Sub UpdateWarehouses()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterWarehouses, datatableWarehouses)
        End If
    End Sub

#End Region

#Region "--LoadingDocks--"

    ''' <summary>Adds a new loading dock to the database</summary>
    ''' <param name="NewLoadingDockObject">The loading dock object to use to add a record to the database</param>
    Public Sub AddNewLoadingDock(ByRef NewLoadingDockObject As dbiWSF.FrameworkObjects.objLoadingDock)
        Dim newDataRow As DataRow = Me.datatableLoadingDocks.NewRow

        Me.ObjectPropertyToDatarow(NewLoadingDockObject, newDataRow, Me.datatableLoadingDocks.Columns, cmapLoadingDocks)

        Me.datatableLoadingDocks.Rows.Add(newDataRow)
        NewLoadingDockObject.AttachedDataRow = newDataRow
        UpdateLoadingDocks()
    End Sub

    ''' <summary>Removes a loading dock from the database</summary>
    ''' <param name="LoadingDockObjectToDelete">The loading dock object to delete from the database</param>
    Public Sub RemoveLoadingDock(ByRef LoadingDockObjectToDelete As dbiWSF.FrameworkObjects.objLoadingDock)
        LoadingDockObjectToDelete.AttachedDataRow.Delete()
        UpdateLoadingDocks()
    End Sub

    ''' <summary>A manual method for updating the loading dock database</summary>
    Public Sub ManualLoadingDockUpdate()
        Try
            dataadapterLoadingDocks.Update(datatableLoadingDocks)
        Catch ex As Exception
            Throw New Exception("Error in ManualLoadingDockUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the loading dock AttachedDataRow with the properties on the loading dock</summary>
    ''' <param name="loadingDockToUpdate">The loading dock object to update</param>
    Public Sub UpdateLoadingDockDataRow(ByRef loadingDockToUpdate As dbiWSF.FrameworkObjects.objLoadingDock)
        If Not loadingDockToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(loadingDockToUpdate, loadingDockToUpdate.AttachedDataRow, Me.datatableLoadingDocks.Columns, cmapLoadingDocks)
            UpdateLoadingDocks()
        End If
    End Sub

    ''' <summary>Supporting Method: Used for updating the loading dock data table when properties are set</summary>
    Private Sub UpdateLoadingDocks()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterLoadingDocks, datatableLoadingDocks)
        End If

    End Sub

    ''' <summary>Supporting Method: Used for removing cargo type keys from the database (loadingdockcargokey table) </summary>
    ''' <param name="CargoTypeID">The CargoTypeID guid to remove from the key table</param>
    ''' <param name="LoadingDockToRemoveFrom">The loading dock object to remove the key from</param>
    Public Sub RemoveLoadingDockCargoKey(ByVal CargoTypeID As System.Guid, ByVal LoadingDockToRemoveFrom As dbiWSF.FrameworkObjects.objLoadingDock)

        Dim GetCargoKeyRow = From selectLoadingDockCargoKeys As DataRow In datatableLoadingDockCargoKeys.Rows _
                             Where ((selectLoadingDockCargoKeys.Item("LoadingDockID") = LoadingDockToRemoveFrom.LoadingDockID) _
                             And (selectLoadingDockCargoKeys.Item("CargoTypeID") = CargoTypeID)) _
                             Select selectLoadingDockCargoKeys

        If GetCargoKeyRow.Count > 0 Then
            GetCargoKeyRow(0).Delete()
        End If
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterLoadingDockCargoKeys, datatableLoadingDockCargoKeys)
        End If

    End Sub

    ''' <summary>Supporting Method: Used for adding cargo type keys from the database (loadingdockcargokey table) </summary>
    ''' <param name="CargoTypeID">The CargoType ID Guid</param>
    ''' <param name="LoadingDockToAddTo">The loading dock object to bind the CargoType to</param>
    Public Sub AddLoadingDockCargoKey(ByVal CargoTypeID As System.Guid, ByVal LoadingDockToAddTo As dbiWSF.FrameworkObjects.objLoadingDock)
        Dim GetCargoKeyRow = From selectLoadingDockCargoKeys As DataRow In datatableLoadingDockCargoKeys.Rows _
                     Where ((selectLoadingDockCargoKeys.Item("LoadingDockID") = LoadingDockToAddTo.LoadingDockID) _
                     And (selectLoadingDockCargoKeys.Item("CargoTypeID") = CargoTypeID)) _
                     Select selectLoadingDockCargoKeys
        If GetCargoKeyRow.Count = 0 Then
            Dim newRow As DataRow = datatableLoadingDockCargoKeys.NewRow
            newRow.Item("LoadingDockCargoID") = System.Guid.NewGuid
            newRow.Item("LoadingDockID") = LoadingDockToAddTo.LoadingDockID
            newRow.Item("CargoTypeID") = CargoTypeID
            Me.datatableLoadingDockCargoKeys.Rows.Add(newRow)

            If AutoUpdateData = True Then
                UpdateIndividualDatatable(dataadapterLoadingDockCargoKeys, datatableLoadingDockCargoKeys)
            End If
        End If
    End Sub

#End Region

#Region "--Shipments--"

    ''' <summary>Adds a new shipment to the database</summary>
    ''' <param name="NewShipmentObject">The shipment object used to create a new record in the database</param>
    Public Sub AddNewShipment(ByRef NewShipmentObject As dbiWSF.FrameworkObjects.objShipment)
        Dim newDataRow As DataRow = Me.datatableShipments.NewRow

        Me.ObjectPropertyToDatarow(NewShipmentObject, newDataRow, Me.datatableShipments.Columns, cmapShipments)

        Me.datatableShipments.Rows.Add(newDataRow)
        NewShipmentObject.AttachedDataRow = newDataRow
        UpdateShipments()

    End Sub

    ''' <summary>Removes a shipment from the database</summary>
    ''' <param name="ShipmentObjectToDelete">The shipment object to delete from the database</param>
    Public Sub RemoveShipment(ByRef ShipmentObjectToDelete As dbiWSF.FrameworkObjects.objShipment)
        ShipmentObjectToDelete.AttachedDataRow.Delete()
        UpdateShipments()
    End Sub

    ''' <summary>A manual method for updating the shipment database</summary>
    Public Sub ManualShipmentUpdate()
        Try
            dataadapterShipments.Update(datatableShipments)
        Catch ex As Exception
            Throw New Exception("Error in ManualShipmentUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the shipment AttachedDataRow with the properties on the shipment</summary>
    ''' <param name="shipmentToUpdate">The shipment object to update</param>
    Public Sub UpdateShipmentDataRow(ByRef shipmentToUpdate As dbiWSF.FrameworkObjects.objShipment)
        If Not shipmentToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(shipmentToUpdate, shipmentToUpdate.AttachedDataRow, Me.datatableShipments.Columns, cmapShipments)
            UpdateShipments()
        End If
    End Sub

    ''' <summary>Supporting Method: Used for updating the shipment data table when properties are set</summary>
    Private Sub UpdateShipments()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterShipments, datatableShipments)
        End If

    End Sub

#End Region

#Region "--OrderHeaders--"

    ''' <summary>Adds a new order header to the database</summary>
    ''' <param name="NewOrderHeaderObject">The order header object used to create a new record in the database</param>
    Public Sub AddNewOrderHeader(ByRef NewOrderHeaderObject As dbiWSF.FrameworkObjects.objOrderHeader)
        Dim newDataRow As DataRow = Me.datatableOrderHeaders.NewRow

        Me.ObjectPropertyToDatarow(NewOrderHeaderObject, newDataRow, Me.datatableOrderHeaders.Columns, cmapOrderHeaders)

        Me.datatableOrderHeaders.Rows.Add(newDataRow)
        NewOrderHeaderObject.AttachedDataRow = newDataRow
        UpdateOrderHeaders()
    End Sub

    ''' <summary>Removes an order header from the database</summary>
    ''' <param name="OrderHeaderObjectToDelete">The order header object to delete from the database</param>
    Public Sub RemoveOrderHeader(ByRef OrderHeaderObjectToDelete As dbiWSF.FrameworkObjects.objOrderHeader)
        OrderHeaderObjectToDelete.AttachedDataRow.Delete()
        UpdateOrderHeaders()
    End Sub

    ''' <summary>A manual method for updating the order header database</summary>
    Public Sub ManualOrderHeaderUpdate()
        Try
            dataadapterOrderHeaders.Update(datatableOrderHeaders)
        Catch ex As Exception
            Throw New Exception("Error in ManualOrderHeaderUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the order header AttachedDataRow with the properties on the order header</summary>
    ''' <param name="orderHeaderToUpdate">The orderheader object to update</param>
    Public Sub UpdateOrderHeaderDataRow(ByRef orderHeaderToUpdate As dbiWSF.FrameworkObjects.objOrderHeader)
        If Not orderHeaderToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(orderHeaderToUpdate, orderHeaderToUpdate.AttachedDataRow, Me.datatableOrderHeaders.Columns, cmapOrderHeaders)
            UpdateOrderHeaders()
        End If
    End Sub

    ''' <summary>Supporting Method: Used for updating the order header data table when properties are set</summary>
    Private Sub UpdateOrderHeaders()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterOrderHeaders, datatableOrderHeaders)
        End If
    End Sub

#End Region

#Region "--OrderDetails--"

    ''' <summary>Adds a new order detail to the database</summary>
    ''' <param name="NewOrderDetailObject">The order detail object used to create a new record in the database</param>
    Public Sub AddNewOrderDetail(ByRef NewOrderDetailObject As dbiWSF.FrameworkObjects.objOrderDetail)
        Dim newDataRow As DataRow = Me.datatableOrderDetails.NewRow

        Me.ObjectPropertyToDatarow(NewOrderDetailObject, newDataRow, Me.datatableOrderDetails.Columns, cmapOrderDetails)

        Me.datatableOrderDetails.Rows.Add(newDataRow)
        NewOrderDetailObject.AttachedDataRow = newDataRow
        UpdateOrderDetails()
    End Sub

    ''' <summary>Removes an order detail from the database</summary>
    ''' <param name="OrderDetailObjectToDelete">The order detail object to delete from the database</param>
    Public Sub RemoveOrderDetail(ByRef OrderDetailObjectToDelete As dbiWSF.FrameworkObjects.objOrderDetail)
        OrderDetailObjectToDelete.AttachedDataRow.Delete()
        UpdateOrderDetails()
    End Sub

    ''' <summary>A manual method for updating the order detail database</summary>
    Public Sub ManualOrderDetailUpdate()
        Try
            dataadapterOrderDetails.Update(datatableOrderDetails)
        Catch ex As Exception
            Throw New Exception("Error in ManualOrderDetailUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the order detail AttachedDataRow with the properties on the order detail</summary>
    ''' <param name="orderDetailToUpdate">The orderdetail object to update</param>
    Public Sub UpdateOrderDetailDataRow(ByRef orderDetailToUpdate As dbiWSF.FrameworkObjects.objOrderDetail)
        If Not orderDetailToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(orderDetailToUpdate, orderDetailToUpdate.AttachedDataRow, Me.datatableOrderDetails.Columns, cmapOrderDetails)
            UpdateOrderDetails()
        End If
    End Sub

    ''' <summary>Supporting Method: Used for updating the order detail data table when properties are set</summary>
    Private Sub UpdateOrderDetails()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterOrderDetails, datatableOrderDetails)
        End If
    End Sub

#End Region

#Region "--CargoTypes--"

    ''' <summary>Adds a new cargo type to the database</summary>
    ''' <param name="NewCargoTypeObject">The cargo type object used to create a new record in the database</param>
    Public Sub AddNewCargoType(ByRef NewCargoTypeObject As dbiWSF.FrameworkObjects.objCargoType)
        Dim newDataRow As DataRow = Me.datatableCargoTypes.NewRow

        Me.ObjectPropertyToDatarow(NewCargoTypeObject, newDataRow, Me.datatableCargoTypes.Columns, cmapCargoTypes)

        Me.datatableCargoTypes.Rows.Add(newDataRow)
        NewCargoTypeObject.AttachedDataRow = newDataRow
        UpdateCargoTypes()
    End Sub

    ''' <summary>Removes a cargo type from the database</summary>
    ''' <param name="CargoTypeObjectToDelete">The cargo type object to delete from the database</param>
    Public Sub RemoveCargoType(ByRef CargoTypeObjectToDelete As dbiWSF.FrameworkObjects.objCargoType)
        CargoTypeObjectToDelete.AttachedDataRow.Delete()
        UpdateCargoTypes()
    End Sub

    ''' <summary>Clears the loading dock cargo keys table for the loading dock provided</summary>
    ''' <param name="LoadingDockToClear">The loading dock exception object to clear the loading docks from</param>
    Public Sub ClearCargoTypesFromLoadingDock(ByRef LoadingDockToClear As dbiWSF.FrameworkObjects.objLoadingDock)
        Try
            Dim delRows() As DataRow = Me.datatableLoadingDockCargoKeys.Select("LoadingDockID = '" + LoadingDockToClear.LoadingDockID.ToString + "'")
            For Each RowToDelete As DataRow In delRows
                RowToDelete.Delete()
            Next
            Me.dataadapterLoadingDockCargoKeys.Update(Me.datatableLoadingDockCargoKeys)
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error clearing LoadingDock Cargo Keys", ex)
        End Try
    End Sub

    ''' <summary>A manual method for updating the cargo type database</summary>
    Public Sub ManualCargoTypeUpdate()
        Try
            dataadapterCargoTypes.Update(datatableCargoTypes)
        Catch ex As Exception
            Throw New Exception("Error in ManualCargoTypeUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the cargo type AttachedDataRow with the properties on the cargo type</summary>
    ''' <param name="cargoTypeToUpdate">The CargoType object to update</param>
    Public Sub UpdateCargoTypeDataRow(ByRef cargoTypeToUpdate As dbiWSF.FrameworkObjects.objCargoType)
        If Not cargoTypeToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(cargoTypeToUpdate, cargoTypeToUpdate.AttachedDataRow, Me.datatableCargoTypes.Columns, cmapCargoTypes)
            UpdateCargoTypes()
        End If
    End Sub

    ''' <summary>Supporting Method: Used for updating the cargo types data table when properties are set</summary>
    Private Sub UpdateCargoTypes()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterCargoTypes, datatableCargoTypes)
        End If

    End Sub

#End Region

#Region "--LoadingDockExceptions--"

    ''' <summary>Adds a new loading dock exception to the database</summary>
    ''' <param name="NewLoadingDockExceptionObject">The loading dock exception object used to create a new record in the database</param>
    Public Sub AddNewLoadingDockException(ByRef NewLoadingDockExceptionObject As dbiWSF.FrameworkObjects.objLoadingDockException)
        Dim newDataRow As DataRow = Me.datatableLoadingDockExceptions.NewRow

        Me.ObjectPropertyToDatarow(NewLoadingDockExceptionObject, newDataRow, Me.datatableLoadingDockExceptions.Columns, cmapLoadingDockExceptions)

        Me.datatableLoadingDockExceptions.Rows.Add(newDataRow)
        NewLoadingDockExceptionObject.AttachedDataRow = newDataRow
        UpdateLoadingDockExceptions()
    End Sub

    ''' <summary>Removes a loading dock exception from the database</summary>
    ''' <param name="LoadingDockExceptionObjectToDelete">The loading dock exception object to delete from the database</param>
    Public Sub RemoveLoadingDockException(ByRef LoadingDockExceptionObjectToDelete As dbiWSF.FrameworkObjects.objLoadingDockException)
        LoadingDockExceptionObjectToDelete.AttachedDataRow.Delete()
        UpdateLoadingDockExceptions()
    End Sub

    ''' <summary>A manual method for updating the loading dock exception database</summary>
    Public Sub ManualLoadingDockExceptionUpdate()
        Try
            dataadapterLoadingDockExceptions.Update(datatableLoadingDockExceptions)
        Catch ex As Exception
            Throw New Exception("Error in ManualLoadingDockExceptionUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the loading dock exception AttachedDataRow with the properties on the cargo type</summary>
    ''' <param name="loadingDockExceptionToUpdate">The loadingdockexception object to update</param>
    Public Sub UpdateLoadingDockExceptionDataRow(ByRef loadingDockExceptionToUpdate As dbiWSF.FrameworkObjects.objLoadingDockException)
        If Not loadingDockExceptionToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(loadingDockExceptionToUpdate, loadingDockExceptionToUpdate.AttachedDataRow, Me.datatableLoadingDockExceptions.Columns, cmapLoadingDockExceptions)
            UpdateLoadingDockExceptions()
        End If
    End Sub

    ''' <summary>Adds a collection of loading dock GUIDs to the loading dock exception keys table</summary>
    ''' <param name="ExceptionToAddDocksTo">The exception to add the loading docks to</param>
    ''' <param name="LoadingDockIDCollection">A list collection of loading dock IDs that this exception will apply to</param>
    Public Sub AddLoadingDocksToException(ByRef ExceptionToAddDocksTo As dbiWSF.FrameworkObjects.objLoadingDockException, ByVal LoadingDockIDCollection As System.Collections.Generic.List(Of System.Guid))
        ClearLoadingDocksFromException(ExceptionToAddDocksTo)

        For Each curLoadingDock As System.Guid In LoadingDockIDCollection
            Dim newKeyRow As DataRow = datatableExceptionDockKeys.NewRow
            newKeyRow("ExceptionDockID") = System.Guid.NewGuid
            newKeyRow("LoadingDockExceptionID") = ExceptionToAddDocksTo.LoadingDockExceptionID
            newKeyRow("LoadingDockID") = curLoadingDock
            datatableExceptionDockKeys.Rows.Add(newKeyRow)
        Next
        dataadapterExceptionDockKeys.Update(datatableExceptionDockKeys)
    End Sub

    ''' <summary>Clears the loading dock exception keys table for the exception provided</summary>
    ''' <param name="LoadingDockExceptionToClear">The loading dock exception object to clear the loading docks from</param>
    Public Sub ClearLoadingDocksFromException(ByRef LoadingDockExceptionToClear As dbiWSF.FrameworkObjects.objLoadingDockException)
        Try
            Dim delRows() As DataRow = datatableExceptionDockKeys.Select("LoadingDockExceptionID = '" + LoadingDockExceptionToClear.LoadingDockExceptionID.ToString + "'")
            For Each RowToDelete As DataRow In delRows
                RowToDelete.Delete()
            Next
            dataadapterExceptionDockKeys.Update(datatableExceptionDockKeys)
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error clearing ExceptionKeys", ex)
        End Try
    End Sub

    ''' <summary>
    ''' Removes a single loading dock from the exception dock keys table.
    ''' </summary>
    ''' <param name="LoadingDockID">The LoadingDockID guid to remove from the table</param>
    ''' <param name="LoadingDockExceptionToRemoveFrom">The loadingdockexception to remove the key from</param>
    ''' <remarks></remarks>
    Public Sub RemoveLoadingDockFromException(ByRef LoadingDockID As System.Guid, ByVal LoadingDockExceptionToRemoveFrom As dbiWSF.FrameworkObjects.objLoadingDockException)
        Try
            Dim RowToDelete() As DataRow = datatableExceptionDockKeys.Select("LoadingDockID = '" + LoadingDockID.ToString + "' AND LoadingDockExceptionID = '" + LoadingDockExceptionToRemoveFrom.LoadingDockExceptionID.ToString + "'")
            RowToDelete(0).Delete()
            dataadapterExceptionDockKeys.Update(datatableExceptionDockKeys)

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error clearing LoadingDock from ExceptionKeys", ex)
        End Try
    End Sub
    ''' <summary>Supporting Method: Used for updating the loading dock exception data table when properties are set</summary>
    Private Sub UpdateLoadingDockExceptions()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterLoadingDockExceptions, datatableLoadingDockExceptions)
        End If
    End Sub

#End Region

#Region "--Recurrences--"

    ''' <summary>Adds a new recurrence to the database</summary>
    ''' <param name="NewRecurrenceObject">The recurrence object used to create a new record in the database</param>
    Public Sub AddNewRecurrence(ByRef NewRecurrenceObject As dbiWSF.FrameworkObjects.objRecurrence)
        Dim newDataRow As DataRow = Me.datatableRecurrences.NewRow

        Me.ObjectPropertyToDatarow(NewRecurrenceObject, newDataRow, Me.datatableRecurrences.Columns, cmapRecurrences)

        Me.datatableRecurrences.Rows.Add(newDataRow)
        NewRecurrenceObject.AttachedDataRow = newDataRow
        UpdateRecurrences()
    End Sub

    ''' <summary>Removes a recurrence from the database</summary>
    ''' <param name="RecurrenceObjectToDelete">The recurrence object to delete from the database</param>
    Public Sub RemoveRecurrence(ByRef RecurrenceObjectToDelete As dbiWSF.FrameworkObjects.objRecurrence)
        RecurrenceObjectToDelete.AttachedDataRow.Delete()
        UpdateRecurrences()
    End Sub

    ''' <summary>A manual method for updating the recurrence database</summary>
    Public Sub ManualRecurrenceUpdate()
        Try
            dataadapterRecurrences.Update(datatableRecurrences)
        Catch ex As Exception
            Throw New Exception("Error in ManualRecurrenceUpdate", ex) ' <GenericError> - Replace as needed
        End Try
    End Sub

    ''' <summary>Updates the recurrence AttachedDataRow with the properties on the cargo type</summary>
    ''' <param name="recurrenceToUpdate">The recurrence object to update</param>
    Public Sub UpdateRecurrenceDataRow(ByRef recurrenceToUpdate As dbiWSF.FrameworkObjects.objRecurrence)
        If Not recurrenceToUpdate.AttachedDataRow Is Nothing Then
            Me.ObjectPropertyToDatarow(recurrenceToUpdate, recurrenceToUpdate.AttachedDataRow, Me.datatableRecurrences.Columns, cmapRecurrences)
            UpdateRecurrences()
        End If
    End Sub

    ''' <summary>Adds a collection of recurrence object GUIDs to the recurrence keys table</summary>
    ''' <param name="RecurrenceToAddObjectsTo">The recurrence object to add the object IDs to</param>
    ''' <param name="ObjectIDCollection">A list collection of GUIDs representing the object IDs this recurrence will apply to</param>
    Public Sub AddObjectsToRecurrence(ByRef RecurrenceToAddObjectsTo As dbiWSF.FrameworkObjects.objRecurrence, ByVal ObjectIDCollection As System.Collections.Generic.List(Of System.Guid))
        ClearObjectsFromRecurrence(RecurrenceToAddObjectsTo)

        For Each curRecurrenceObjectID As System.Guid In ObjectIDCollection
            Dim newKeyRow As DataRow = datatableRecurrenceObjectKeys.NewRow
            newKeyRow("RecurrenceObjectID") = System.Guid.NewGuid
            newKeyRow("RecurrenceID") = RecurrenceToAddObjectsTo.RecurrenceID
            newKeyRow("ObjectID") = curRecurrenceObjectID
            datatableRecurrenceObjectKeys.Rows.Add(newKeyRow)
        Next
        dataadapterRecurrenceObjectKeys.Update(datatableRecurrenceObjectKeys)
    End Sub

    ''' <summary>Clears the recurrence object keys table for the recurrence provided</summary>
    ''' <param name="RecurrenceObjectToClearIDsFrom">The recurrence object to clear the object IDs from</param>
    Public Sub ClearObjectsFromRecurrence(ByRef RecurrenceObjectToClearIDsFrom As dbiWSF.FrameworkObjects.objRecurrence)
        Try
            Dim delRows() As DataRow = datatableRecurrenceObjectKeys.Select("RecurrenceID = '" + RecurrenceObjectToClearIDsFrom.RecurrenceID.ToString + "'")
            For Each RowToDelete As DataRow In delRows
                RowToDelete.Delete()
            Next
            dataadapterRecurrenceObjectKeys.Update(datatableRecurrenceObjectKeys)
        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error clearing RecurrenceKeys", ex)
        End Try
    End Sub

    ''' <summary>
    ''' Removes a recurrenceobject from the recurrenceobject keys table.
    ''' </summary>
    ''' <param name="RecurrenceObjectID">The recurrenceobjectID guid to remove</param>
    ''' <param name="RecurrenceToRemoveFrom">The recurrence object to remove the recurrenceobject from</param>
    ''' <remarks></remarks>
    Public Sub RemoveRecurrenceObjectFromRecurrence(ByRef RecurrenceObjectID As System.Guid, ByVal RecurrenceToRemoveFrom As dbiWSF.FrameworkObjects.objRecurrence)
        Try
            Dim RowToDelete() As DataRow = datatableRecurrenceObjectKeys.Select("ObjectID = '" + RecurrenceObjectID.ToString + "' AND RecurrenceID = '" + RecurrenceToRemoveFrom.RecurrenceID.ToString + "'")
            RowToDelete(0).Delete()
            dataadapterRecurrenceObjectKeys.Update(datatableRecurrenceObjectKeys)

        Catch ex As Exception
            '<Generic Error> - Change as needed
            Throw New Exception("Error clearing RecurrenceObject from RecurrenceKeys", ex)
        End Try
    End Sub

    ''' <summary>Supporting Method: Used for updating the recurrence data table when properties are set</summary>
    Private Sub UpdateRecurrences()
        If AutoUpdateData = True Then
            UpdateIndividualDatatable(dataadapterRecurrences, datatableRecurrences)
        End If
    End Sub

#End Region

#End Region

End Class

